We know that in the Spring Boot inherit WebMvcConfigurerAdapter with their own configuration class after 2.0, idea will prompt the class is obsolete.
Normally we use two alternative of the following:
- Achieve WebMvcConfigurer
- Inheritance WebMvcConfigurationSupport
But found cause some problems inherited WebMvcConfigurationSupport
Prior to this, we look at WebMvc automatic configuration WebMvcAutoConfiguration class definition:
Note that the red line to ring up the key statement:
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
See this line can understand, the original SpringBoot made this restriction only when WebMvcConfigurationSupport class does not exist to take effect WebMvc automated configuration, automatic configuration WebMvc class defines not only the classpath: / META-INF / resources /, classpath: / resources /, classpath: / static /, classpath: mapping / public / other path, the configuration information also defines the beginning of the file spring.mvc like.
When WebMvcAutoConfiguration not take effect will lead to the following questions:
1.WebMvcProperties and ResourceProperties failure
Since the properties of two classes are arranged in the WebMvcAutoConfiguration
When WebMvc autoconfiguration fails (WebMvcAutoConfiguration automated configuration), it will lead to not view and return to the resolver can not resolve the corresponding view
solution:
1. SpringBoot1.X version, we can inherit from WebMvcConfigurerAdapter, covering methods to want to achieve.
2. However, in the definition of SpringBoot2.X in, WebMvcConfigurerAdapter has been defined as @Deprecated, we look at the source code:
SpringBoot in fact already told you WebMvcConfigurerAdapter since the beginning of Spring5.0 version is no longer recommended, but you can achieve WebMvcConfigurer and override the relevant methods to achieve similar functionality.
2. HttpMessageConverter the class path failure
Such as: StringHttpMessageConverterConfiguration, MappingJackson2HttpMessageConverter, because HttpMessageConverters held in all the instances of HttpMessageConverter in WebMvcAutoConfigurationAdapter will inject HttpMessageConverters, so when WebMvcAutoConfigurationAdapter not loaded, it will fail, resulting in indirect spring.http.encoding.charset and spring.jackson. date-format illusion of failure.
Such as: StringHttpMessageConverter spring.http.encoding.charset use configuration, is a default encoding: ISO-8859-1
@Bean @ConditionalOnMissingBean public StringHttpMessageConverter stringHttpMessageConverter() { StringHttpMessageConverter converter = new StringHttpMessageConverter( this.encodingProperties.getCharset()); converter.setWriteAcceptCharset(false); return converter; }
Solution :
Because the configuration classes are known by @Bean registered in the container, then we can use WebMvcConfigurationSupport, by @Autowired injected into the configuration class, and then replace the default configuration can WebMvcConfigurationSupport, such as:
@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); } } } }
It can also re-implemented according to demand.
supplement_
WebMvcConfigurer class code Analysis:
Starting EnableWebMvcConfiguration class configuration, when it is injected into the parent class will inject all WebMvcConfigurer Spring container
Then it will inject RequestMappingHandlerAdapter Bean calls the parent class method requestMappingHandlerAdapter
This time to get the configuration, such as custom return results Handler, WebMvcConfigurerComposite will traverse call WebMvcConfigurer implementation class
addReturnValueHandlers method, custom configuration will load into the default configuration