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"))); } }
原文地址:https://www.cnblogs.com/mrlliuchao/p/10305409.html
时间: 2024-10-04 18:06:44