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

 

posted @ 2020-06-12 20:07  海绵般汲取  阅读(863)  评论(0编辑  收藏  举报