SpringBoot2繼承了WebMvcConfigurationSupport後,WebMvcAutoConfiguration自動配置失效
阿新 • • 發佈:2019-01-22
rri classpath control ati resource class inter .config fig
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/"); } /** * 頁面跳轉 */ @Overrideprotected 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"))); } }
SpringBoot2繼承了WebMvcConfigurationSupport後,WebMvcAutoConfiguration自動配置失效