WebMvcConfigurer 與 WebMvcConfigurationSupport的坑


在Spring Boot 2.0后用自己的的配置類繼承WebMvcConfigurerAdapter時,idea會提示這個類已經過時了。通常情況下我們會采用下面兩種代替方案:

  • 實現WebMvcConfigurer  
  • 繼承WebMvcConfigurationSupport

但是繼承WebMvcConfigurationSupport時發現會造成一些問題。

在這之前,我們先看一下WebMvc自動配置類WebMvcAutoConfiguration的定義:

注意紅框圈起來到這個關鍵語句:

@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)

看到這行可以明白,原來SpringBoot做了這個限制,只有當WebMvcConfigurationSupport類不存在的時候才會生效WebMvc自動化配置,WebMvc自動配置類中不僅定義了classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/等路徑的映射,還定義了配置文件spring.mvc開頭的配置信息等。

當 WebMvcAutoConfiguration 不生效時會導致以下幾個問題:

1. WebMvcProperties 和 ResourceProperties 失效

因為兩個配置類中的屬性都在 WebMvcAutoConfiguration 中使用。當WebMvc自動配置失效(WebMvcAutoConfiguration自動化配置)時,會導致無法視圖解析器無法解析並返回到對應的視圖。

解決方案:

(1) 在SpringBoot1.X的版本中,我們可以繼承自WebMvcConfigurerAdapter,覆蓋想要實現的方法即可。

(2) 但是在SpringBoot2.X的定義中,WebMvcConfigurerAdapter已經被定義為@Deprecated,我們來看一下源代碼:

SpringBoot其實也已經告訴你WebMvcConfigurerAdapter自從Spring5.0版本開始已經不建議使用了,但是你可以實現WebMvcConfigurer並重寫相關方法來達到類似的功能。

2. 類路徑上的 HttpMessageConverter 失效

如:StringHttpMessageConverterConfiguration、MappingJackson2HttpMessageConverter ,因為 HttpMessageConverters 中持有着所有HttpMessageConverter的實例, 在 WebMvcAutoConfigurationAdapter 中會注入 HttpMessageConverters ,因此當 WebMvcAutoConfigurationAdapter 不加載時,則會失效,間接的造成 spring.http.encoding.charset 與 spring.jackson.date-format 假象的失效。

如:StringHttpMessageConverter 會使用 spring.http.encoding.charset 配置, 默認編碼為:ISO-8859-1

@Bean
@ConditionalOnMissingBean
public StringHttpMessageConverter stringHttpMessageConverter() {
    StringHttpMessageConverter converter = new StringHttpMessageConverter(
            this.encodingProperties.getCharset());
    converter.setWriteAcceptCharset(false);
    return converter;
}

解決方案:

因為已知的配置類都已通過 @Bean 注冊在容器中,那么我們可以在使用 WebMvcConfigurationSupport 時,通過 @Autowired 注入到配置類中,然后替換掉 WebMvcConfigurationSupport 默認的配置即可,如:

@Configuration
public class WebConfiguration extends WebMvcConfigurationSupport {
    @Autowired
    private StringHttpMessageConverter stringHttpMessageConverter;
    @Autowired
    private MappingJackson2HttpMessageConverter httpMessageConverter;
    /**
     * 添加轉換器
     */
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        for (int i = 0; i < converters.size(); i++) {
            if (converters.get(i) instanceof StringHttpMessageConverter){
                converters.set(i, stringHttpMessageConverter);
            }
            if (converters.get(i) instanceof MappingJackson2HttpMessageConverter) {
                converters.set(i, httpMessageConverter);
            }
        }
    }
}

也可以根據需求來重新實現。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM