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注解去掉之后就可以正常的

  • 相关阅读:
    sql server 日期格式化
    DPDK latencystats库使用方案
    PPTP协议握手流程分析--转载
    北京联通IPTV 数码视讯 Q1 破解过程
    Linux发不出分片包的问题分析
    近期团队比较动荡
    ab输出信息解释以及Failed requests原因分析
    salt源码安装
    imuxsock lost 353 messages from pid 20261 due to rate-limiting 解决办法
    解决vue路由history模式刷新后404的问题
  • 原文地址:https://www.cnblogs.com/gne-hwz/p/13109955.html
Copyright © 2011-2022 走看看