主题:beetl与jsp整合后,@ResponseBody 出现404问题

cloudsea 2018年01月12日 456

springboot 集成beetl ,原来有jsp配置,beetl使用默认配置。单独返回/**.btl由beetl处理,其它的由jsp处理。但@ResponseBody时出现404问题,返回还是走jsp处理。测试发现配置beetl.enabled=false时@ResponseBody正常

@Configuration//@EnableWebMvc,加上此行,则spring boot的默认mvc配置失效,需要自己配置所有@ConditionalOnWebApplication@ConditionalOnClass({Servlet.class, DispatcherServlet.class})
@AutoConfigureBefore(WebMvcAutoConfiguration.class)
@EnableConfigurationProperties(ResourceProperties.class)
public class WebApplicationConfig extends WebMvcConfigurerAdapter {
    @Autowired(required = false)
    private List<ErrorViewResolver> errorViewResolvers;
    private final ServerProperties serverProperties;

    public WebApplicationConfig(
            ServerProperties serverProperties) {
        this.serverProperties = serverProperties;
    }

    //全局的异常处理    @Bean    public MyBasicErrorController basicErrorController(ErrorAttributes errorAttributes) {
        return new MyBasicErrorController(errorAttributes, this.serverProperties.getError(),
                this.errorViewResolvers);
    }

    @Bean    public WebControllerInterceptor webControllerInterceptorBeanCreated(){
        return new WebControllerInterceptor();
    }

    @Override    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);
        //支持fastjson解析        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig config = new FastJsonConfig();
        config.setSerializerFeatures(SerializerFeature.PrettyFormat,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullStringAsEmpty,
                SerializerFeature.WriteDateUseDateFormat        );
        fastJsonHttpMessageConverter.setFastJsonConfig(config);
        converters.add(fastJsonHttpMessageConverter);
    }

    @Override    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new UserInterceptor()).addPathPatterns("/user/**");
        super.addInterceptors(registry);
    }

    @Override    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(new LoginUserWebArgumentResolver());
        argumentResolvers.add(new EntityHandlerMethodArgumentResolver());
        argumentResolvers.add(new HttpViewHandlerMethodArgumentResolver());
        argumentResolvers.add(new QueryCriteriaHandlerMethodArgumentResolver());
        super.addArgumentResolvers(argumentResolvers);
    }

    @Override    public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {
        super.addReturnValueHandlers(returnValueHandlers);
    }
}
闲大赋 2018年01月14日

这个有点奇怪了,按理说@ResponseBody ,说明Controller的反回结果会序列输出,不会交给模板引擎