Spring Boot 中文乱码解决
2017-10-23 18:10
579 查看
1. 第一步,约定传参编码格式
不管是使用httpclient,还是okhttp,都要设置传参的编码,为了统一,这里全部设置为utf-8第二步,修改application.properties文件
增加如下配置:spring.http.encoding.force=true spring.http.encoding.charset=UTF-8 spring.http.encoding.enabled=true server.tomcat.uri-encoding=UTF-8
此时拦截器中返回的中文已经不乱码了,但是controller中返回的数据依旧乱码。
第三步,修改controller的@RequestMapping
修改如下:produces="text/plain;charset=UTF-8"
这种方法的弊端是限定了数据类型,继续查找资料,在stackoverflow上发现解决办法,是在配置类中增加如下代码:
@Configuration public class CustomMVCConfiguration extends WebMvcConfigurerAdapter { @Bean public HttpMessageConverter<String> responseBodyConverter() { StringHttpMessageConverter converter = new StringHttpMessageConverter( Charset.forName("UTF-8")); return converter; } @Override public void configureMessageConverters( List<HttpMessageConverter<?>> converters) { super.configureMessageConverters(converters); converters.add(responseBodyConverter()); } @Override public void configureContentNegotiation( ContentNegotiationConfigurer configurer) { configurer.favorPathExtension(false); } }
相关文章推荐
- Springboot 之 解决IDEA读取properties配置文件的中文乱码问题
- 解决Springboot使用FastJson 返回中文乱码的问题
- Spring Boot/Spring Cloud解决中文乱码
- spring boot 解决后台返回 json 到前台中文乱码之后出现返回json数据报错 500
- Springboot使用FastJson后,接口返回中文乱码的问题解决。
- Springboot 之 解决IDEA读取properties配置文件的中文乱码问题
- Springboot解决IDEA读取properties配置文件的中文乱码
- Spring Boot 中文乱码解决
- 解决SpringBoot整合Activiti5.20.0流程图中文乱码
- Springboot 之 解决IDEA读取properties配置文件的中文乱码问题
- SpringBoot JPA MySQL 遇到中文乱码的解决办法
- spring boot新手教程之使用FastJson解析JSON数据以及解决返回中文乱码问题
- 解决SpringBoot框架下利用jdbcTemplate插入mysql数据库中文乱码问题
- SpringBoot 用fastjson替换到jackjson并解决中文乱码的问题
- spring boot jpa mysql 解决中文乱码
- SpringBoot mail中文附件乱码的解决方法
- Springboot 之 解决IDEA读取properties配置文件的中文乱码问题
- Springboot 之 解决IDEA读取properties配置文件的中文乱码问题
- 关于Spring boot中读取属性配置文件出现中文乱码的问题的解决
- 解决Spring MVC @ResponseBody返回中文字符串乱码问题