后端返回中文數據亂碼
之前使用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注解去掉之后就可以正常的