zoukankan      html  css  js  c++  java
  • springBoot项目配置错误问题-@EnableWebMvc注解问题导致SpringBoot默认配置失效

    后端返回中文数据乱码

    之前使用springBoot的默认配置时,中文数据还是能够正常显示

    在我加了日志拦截器之后,就显示中文乱码了

    测试代码

    @Controller
    @RequestMapping("/a/test")
    public class TestController {
        Logger logger= LoggerFactory.getLogger(TestController.class);
        @RequestMapping(value = "index")
        @ResponseBody
        public String index(DataMax dataMax){
            logger.info("测试log日志运行");
            return dataMax.getDataType();
        }
    }

     测试发现在返回响应内容里,会指定字符编码为charset=ISO-8859-1

    因为没加拦截器之前好好的,所以就锁定位置,经过网上查询,是因为我在日志拦截器上加了@EnableWebMvc注解导致了

    @Configuration
    @EnableWebMvc
    public class LogInterceptorConfig implements WebMvcConfigurer {
    
        @Autowired
        WebInterceptorProperties webInterceptorProperties;
    }

    springBoot 文档有一段话

    Finally, if you opt out of the Spring Boot default MVC configuration by providing your own @EnableWebMvc configuration, you can take control completely and do everything manually by using getMessageConverters from WebMvcConfigurationSupport.

    《Spring Boot Reference Guide》

    最后,如果您通过提供自己的@EnableWebMvc配置选择退出Spring Boot默认MVC配置,

    则可以完全控制并使用WebMvcConfigurationSupport中的getMessageConverters手动完成所有操作。

    我加了这个注解,导致springBoot相关的默认配置不生效,而是使用Spring自带的默认配置。

    将@EnableWebMvc注解去掉之后就可以正常的

  • 相关阅读:
    CSS布局之盒子模型[二]
    CSS布局之盒子模型[一]
    CSS文本相关之垂直排列[5]
    网站迁移之后,中文路径都变成乱码
    Linux中shell搜索多文件中的字符串
    mysql数据库报错
    使用Flarum轻松搭建自己的论坛
    CSS雪碧图-html优化
    CSS-定位模式
    ul当做div标签的使用
  • 原文地址:https://www.cnblogs.com/gne-hwz/p/13109955.html
Copyright © 2011-2022 走看看