1. 程式人生 > 其它 >springMvc 之 WebMvcConfigurer詳解

springMvc 之 WebMvcConfigurer詳解

目錄

 

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