目錄
1. 簡介
2. WebMvcConfigurer接口
2.1 addInterceptors:攔截器
2.2 addViewControllers:頁面跳轉
2.3 addResourceHandlers:靜態資源
2.4 configureDefaultServletHandling:默認靜態資源處理器
2.5 configureViewResolvers:視圖解析器
2.6 configureContentNegotiation:配置內容裁決的一些參數
2.7 addCorsMappings:跨域
2.8 configureMessageConverters:信息轉換器
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
2. WebMvcConfigurer接口
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();
}
常用的方法:
/* 攔截器配置 */
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.1 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.2 addViewControllers:頁面跳轉
以前寫SpringMVC的時候,如果需要訪問一個頁面,必須要寫Controller類,然后再寫一個方法跳轉到頁面,感覺好麻煩,其實重寫WebMvcConfigurer中的addViewControllers方法即可達到效果了
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/toLogin").setViewName("login");
}
值的指出的是,在這里重寫addViewControllers方法,並不會覆蓋WebMvcAutoConfiguration(Springboot自動配置)中的addViewControllers(在此方法中,Spring Boot將“/”映射至index.html),這也就意味着自己的配置和Spring Boot的自動配置同時有效,這也是我們推薦添加自己的MVC配置的方式。
2.3 addResourceHandlers:靜態資源
比如,我們想自定義靜態資源映射目錄的話,只需重寫addResourceHandlers方法即可。
注:如果繼承WebMvcConfigurationSupport類實現配置時必須要重寫該方法
@Configuration
public class MyWebMvcConfigurerAdapter implements WebMvcConfigurer {
/**
* 配置靜態訪問資源
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/my/**").addResourceLocations("classpath:/my/");
}
}
- addResoureHandler:指的是對外暴露的訪問路徑
- addResourceLocations:指的是內部文件放置的目錄
2.4 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是幫助回歸這個特性的。
2.5 configureViewResolvers:視圖解析器
這個方法是用來配置視圖解析器的,該方法的參數ViewResolverRegistry 是一個注冊器,用來注冊你想自定義的視圖解析器等。ViewResolverRegistry 常用的幾個方法:https://blog.csdn.net/fmwind/article/details/81235401
/**
* 配置請求視圖映射
* @return
*/
@Bean
public InternalResourceViewResolver resourceViewResolver()
{
InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
//請求視圖文件的前綴地址
internalResourceViewResolver.setPrefix("/WEB-INF/jsp/");
//請求視圖文件的后綴
internalResourceViewResolver.setSuffix(".jsp");
return internalResourceViewResolver;
}
/**
* 視圖配置
* @param registry
*/
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
super.configureViewResolvers(registry);
registry.viewResolver(resourceViewResolver());
/*registry.jsp("/WEB-INF/jsp/",".jsp");*/
}
2.6 configureContentNegotiation:配置內容裁決的一些參數
2.7 addCorsMappings:跨域
@Override
public void addCorsMappings(CorsRegistry registry) {
super.addCorsMappings(registry);
registry.addMapping("/cors/**")
.allowedHeaders("*")
.allowedMethods("POST","GET")
.allowedOrigins("*");
}
2.8 configureMessageConverters:信息轉換器
/**
* 消息內容轉換配置
* 配置fastJson返回json轉換
* @param converters
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//調用父類的配置
super.configureMessageConverters(converters);
//創建fastJson消息轉換器
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//創建配置類
FastJsonConfig fastJsonConfig = new FastJsonConfig();
//修改配置返回內容的過濾
fastJsonConfig.setSerializerFeatures(
SerializerFeature.DisableCircularReferenceDetect,
SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullStringAsEmpty
);
fastConverter.setFastJsonConfig(fastJsonConfig);
//將fastjson添加到視圖消息轉換器列表內
converters.add(fastConverter);
}
參考:https://blog.csdn.net/fmwind/article/details/81235401
參考:https://blog.csdn.net/weixin_43453386/article/details/83623242#1addInterceptors_32
