zoukankan      html  css  js  c++  java
  • 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);
                }
            }
        }
    }

    也可以根据需求来重新实现。

  • 相关阅读:
    单调栈
    LRU先进先出算法
    IntelliJ IDEA 的安装
    IntelliJ IDEA 的介绍
    Tampermonkey 油猴脚本例子2
    Tampermonkey 油猴脚本例子
    Python PIL.Image 图片操作
    Python os 备查
    Pytorch tensor 常用操作 备查
    Pytorch torchvision.utils.save_image 图片保存工具
  • 原文地址:https://www.cnblogs.com/myitnews/p/12329126.html
Copyright © 2011-2022 走看看