SpringBoot2继承了WebMvcConfigurationSupport后,WebMvcAutoConfiguration自动配置失效

SpringBoot1继承的WebMvcConfigurerAdapter,在 2 中废弃了

SpringBoot2继承了WebMvcConfigurationSupport 后,WebMvcAutoConfiguration自动配置失效,需要全部手动配置

@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
    /**
     * 静态资源映射
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        // addResourceHandler 设置访问路径前缀,addResourceLocations 设置资源路径
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");

    }

    /**
     * 页面跳转
     */
    @Override
    protected void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("index").setViewName("index");
    }

    /**
     * 拦截器配置
     */
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/goLogin", "/login");
    }

    /**
     * 编码配置
     */
    @Override
    protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);
        converters.add(new StringHttpMessageConverter(Charset.forName("UTF-8")));
    }
}

 

posted @ 2019-01-22 18:52  菜鸟_L  阅读(1328)  评论(0编辑  收藏  举报