1. 前言 WebMvcConfigurer配置類其實是Spring內部的一種配置方式,採用JavaBean的形式來代替傳統的xml配置文件形式進行針對框架個性化定製,可以自定義一些Handler,Interceptor,ViewResolver,MessageConverter。基於java-ba ...
1. 前言
WebMvcConfigurer配置類其實是Spring內部的一種配置方式,採用JavaBean的形式來代替傳統的xml配置文件形式進行針對框架個性化定製,可以自定義一些Handler,Interceptor,ViewResolver,MessageConverter。基於java-based方式的spring mvc配置,需要創建一個配置類並實現WebMvcConfigurer 介面;
在Spring Boot 1.5版本都是靠重寫WebMvcConfigurerAdapter的方法來添加自定義攔截器,消息轉換器等。SpringBoot 2.0 後,該類被標記為@Deprecated(棄用)。官方推薦直接實現WebMvcConfigurer或者直接繼承WebMvcConfigurationSupport,方式一實現WebMvcConfigurer介面(推薦),方式二繼承WebMvcConfigurationSupport類,具體實現可看這篇文章。https://blog.csdn.net/fmwind/article/details/82832758
衝突問題
SpringBoot添加Interceptor後addInterceptors方法不執行,攔截器不生效
原因其實很簡單,因為代碼中有【WebMvcConfigurationSupport】的繼承類,SpringBoot會判斷,如果有【WebMvcConfigurationSupport】就不會載入【WebMvcConfigurer】。
2. WebMvcConfigurer介面說明
2.1:原碼
public interface WebMvcConfigurer {
void configurePathMatch(PathMatchConfigurer var1);
void configureContentNegotiation(ContentNegotiationConfigurer var1);
void configureAsyncSupport(AsyncSupportConfigurer var1);
void configureDefaultServletHandling(DefaultServletHandlerConfigurer var1);
void addFormatters(FormatterRegistry var1);
void addInterceptors(InterceptorRegistry var1);
void addResourceHandlers(ResourceHandlerRegistry var1);
void addCorsMappings(CorsRegistry var1);
void addViewControllers(ViewControllerRegistry var1);
void configureViewResolvers(ViewResolverRegistry var1);
void addArgumentResolvers(List<HandlerMethodArgumentResolver> var1);
void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> var1);
void configureMessageConverters(List<HttpMessageConverter<?>> var1);
void extendMessageConverters(List<HttpMessageConverter<?>> var1);
void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> var1);
void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> var1);
Validator getValidator();
MessageCodesResolver getMessageCodesResolver();
}
2.2:常用的方法:
/* 攔截器配置 */
void addInterceptors(InterceptorRegistry var1);
/* 視圖跳轉控制器 */
void addViewControllers(ViewControllerRegistry registry);
/**
*靜態資源處理
**/
void addResourceHandlers(ResourceHandlerRegistry registry);
/* 預設靜態資源處理器 */
void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer);
/**
* 這裡配置視圖解析器
**/
void configureViewResolvers(ViewResolverRegistry registry);
/* 配置內容裁決的一些選項*/
void configureContentNegotiation(ContentNegotiationConfigurer configurer);
/** 解決跨域問題 **/
public void addCorsMappings(CorsRegistry registry) ;
2.3 addInterceptors:攔截器
addInterceptor:需要一個實現HandlerInterceptor介面的攔截器實例
addPathPatterns:用於設置攔截器的過濾路徑規則;addPathPatterns("/**")對所有請求都攔截
excludePathPatterns:用於設置不需要攔截的過濾規則
攔截器主要用途:進行用戶登錄狀態的攔截,日誌的攔截等。
@Override
public void addInterceptors(InterceptorRegistry registry) {
super.addInterceptors(registry);
registry.addInterceptor(new TestInterceptor()).addPathPatterns("/**").excludePathPatterns("/emp/toLogin","/emp/login","/js/**","/css/**","/images/**");
}
2.4 addViewControllers:頁面跳轉, 比如登錄跳轉或錯誤頁面跳轉
以前寫SpringMVC的時候,如果需要訪問一個頁面,必須要寫Controller類,然後再寫一個方法跳轉到頁面,感覺好麻煩,其實重寫WebMvcConfigurer中的addViewControllers方法即可達到效果了
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/toLogin").setViewName("login");
registry.addViewController("/403").setViewName("/403");
registry.addViewController("/404").setViewName("/404");
}
值的指出的是,在這裡重寫addViewControllers方法,並不會覆蓋WebMvcAutoConfiguration(Springboot自動配置)中的addViewControllers(在此方法中,Spring Boot將“/”映射至index.html),這也就意味著自己的配置和Spring Boot的自動配置同時有效,這也是我們推薦添加自己的MVC配置的方式。
2.5 addResourceHandlers:靜態資源
比如,我們想自定義靜態資源映射目錄的話,只需重寫addResourceHandlers方法即可。
註:如果繼承WebMvcConfigurationSupport類實現配置時必須要重寫該方法,具體見其它文章
@Configuration
public class MyWebMvcConfigurerAdapter implements WebMvcConfigurer {
/**
* 配置靜態訪問資源
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/resources")
.addResourceLocations("classpath:/static")
.addResourceLocations("classpath:/templates")
.addResourceLocations("classpath:/cert")
.addResourceLocations("classpath:/ueditor")
.addResourceLocations("classpath:/shiro");
registry.addResourceHandler("swagger-ui.html").addResourceLocations(
"classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations(
"classpath:/META-INF/resources/webjars/");
registry.addResourceHandler("/workflow/**").addResourceLocations("classpath:/static/workflow/");
super.addResourceHandlers(registry);
}
}
addResoureHandler:指的是對外暴露的訪問路徑
addResourceLocations:指的是內部文件放置的目錄
2.6 configureDefaultServletHandling:預設靜態資源處理器
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
configurer.enable("defaultServletName");
}
此時會註冊一個預設的Handler:DefaultServletHttpRequestHandler,這個Handler也是用來處理靜態文件的,它會嘗試映射/。當DispatcherServelt映射/時(/ 和/ 是有區別的),並且沒有找到合適的Handler來處理請求時,就會交給DefaultServletHttpRequestHandler 來處理。註意:這裡的靜態資源是放置在web根目錄下,而非WEB-INF 下。
可能這裡的描述有點不好懂(我自己也這麼覺得),所以簡單舉個例子,例如:在webroot目錄下有一個圖片:1.png 我們知道Servelt規範中web根目錄(webroot)下的文件可以直接訪問的,但是由於DispatcherServlet配置了映射路徑是:/ ,它幾乎把所有的請求都攔截了,從而導致1.png 訪問不到,這時註冊一個DefaultServletHttpRequestHandler 就可以解決這個問題。其實可以理解為DispatcherServlet破壞了Servlet的一個特性(根目錄下的文件可以直接訪問),DefaultServletHttpRequestHandler是幫助回歸這個特性的。
作者:倉儲大叔,張占嶺,
榮譽:微軟MVP
QQ:853066980
支付寶掃一掃,為大叔打賞!