spring boot 中文乱码问题
在刚接触spring boot 2.0的时候,遇到了一些中文乱码的问题,网上找了一些解决方法。
这里自己做个汇总。
在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中返回的数据依旧乱码。
需要在@RequestMapping(这一类定义请求路径的注解)中增加produces="text/plain;charset=UTF-8"
但是这样就得要限定请求的数据类型,并且需要在每个请求里都加上这个,工作比较繁杂。
可以自己写一个配置文件类,继承WebMvcConfigurationSupport,代码如下:
@Configuration
public class CustomMVCConfiguration extends WebMvcConfigurationSupport{ @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);
}
}
基本上就解决了乱码问题。
如果出现org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class java.util.LinkedHashMap
就需要在配置类中,添加一些代码,完整的代码如下:
@Configuration
public class InterceptorConfig extends WebMvcConfigurationSupport { /**
* 解决中文乱码问题
*/
@Bean
public HttpMessageConverter<String> responseBodyConverter() {
return new StringHttpMessageConverter(Charset.forName("UTF-8"));
} @Bean
public ObjectMapper getObjectMapper() {
return new ObjectMapper();
} @Bean
public MappingJackson2HttpMessageConverter messageConverter() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setObjectMapper(getObjectMapper());
return converter;
} @Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
//解决中文乱码
converters.add(responseBodyConverter());
//解决 添加解决中文乱码后 上述配置之后,返回json数据直接报错 500:no convertter for return value of type
converters.add(messageConverter());
converters.add(responseBodyConverter());
} @Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(false);
}
}
spring boot 中文乱码问题的更多相关文章
- Spring Boot 中文乱码问题解决方案汇总
使用 Spring Boot 开发,对外开发接口供调用,传入参数中有中文,出现中文乱码,查了好多资料,总结解决方法如下: 第一步,约定传参编码格式 不管是使用httpclient,还是okhttp,都 ...
- 解决spring boot中文乱码问题
在开发或学习当中,我们不可避免的会碰到中文乱码的问题(好想哭,但还是要保持微笑!) 今天,在学习spring boot中碰到了中文乱码问题. 首先,看了一下workspace是不是设置utf-8默认字 ...
- spring boot: 中文显示乱码,在applicationContext里面配置
spring boot: 中文显示乱码,在applicationContext里面配置 applicationContext.properties ########################## ...
- spring boot + thymeleaf 乱码问题
spring boot + thymeleaf 乱码问题 hellotrms 发布于 2017/01/17 15:27 阅读 1K+ 收藏 0 答案 1 开发四年只会写业务代码,分布式高并发都不会还做 ...
- spring 解决中文乱码问题
spring 解决中文乱码问题 使用spring的前提下在web.xml中配置 <filter> <filter-name>encodingFilter</filter- ...
- 彻底解决Spring MVC 中文乱码 问题
1:表单提交controller获得中文参数后乱码解决方案 注意: jsp页面编码设置为UTF-8 form表单提交方式为必须为post,get方式下面spring编码过滤器不起效果 <%@ p ...
- spring mvc 中文乱码 post与get的方法解决
spring mvc表单提交中文参数乱码问题 今天测试spring mvc ,中文乱码,在web.xml中加上 <filter> <filter-name>encodingF ...
- Spring MVC 中文乱码的解决
对于POST方法提交的中文乱码 , 可在web.xml中添加如下代码 : <filter> <filter-name>encodingFilter</filter-nam ...
- spring MVC中文乱码相关总结
总结几种方式,都使用的话能解决大多数乱码的情况 1.所有页面使用 <%@page language="java" pageEncoding="UTF-8" ...
随机推荐
- windows用交互式命令执行python程序
1.进入cmd命令 windows+r2.进入盘符,eg:E:3.使用dir命令查看当前文件夹下的所有目录4.使用绝对路径或者相对路径和cd命令直接进入想要到达的文件夹目录(或者使用cd命令一步一步达 ...
- thinkphp5访问sql2000数据库
大家都知道php跟mysql是绝配,但是因为有时候工作需要,要求php访问操作sql2000,怎么办呢? 一般来说有两种方式: 1. sqlsrv驱动方式 2. odbc方式 sqlsrv驱动方式,因 ...
- Linux下Bash shell学习笔记
原文地址: http://www.cnblogs.com/NickQ/p/8870423.html 1.shell下没有变量类型和定义的概念. 变量直接使用不用定义 所有值都视为字符串. 在对变量取值 ...
- Linux内核调用I2C驱动_驱动嵌套驱动方法
禁止转载!!!! Linux内核调用I2C驱动_以MPU6050为例 0. 导语 最近一段时间都在恶补数据结构和C++,加上导师的事情比较多,Linux内核驱动的学习进程总是被阻碍.不过,十一假期终于 ...
- 从零开始一个http服务器(四)-动态返回
从零开始一个http服务器(四) 代码地址 : https://github.com/flamedancer/cserver git checkout step4 运行: make clean &am ...
- BZOJ1011 莫比乌斯反演(基础题
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1101 [题目大意] 求[1,n][1,m]内gcd=k的情况 [题解] 考虑求[1,n ...
- C# set 跟 get
可以在类里面 private string name; public string Name { get { return name; } set { name = value; } }
- 使用java实现AES加密
公司最近做agent项目,需要对一些远程重要的请求参数进行加密.加密之前选型,选择了AES,而DES算法加密,容易被破解.网上有很多关于加密的算法的Demo案列,我发现这些Demo在Window平台运 ...
- elk6.3.2在线安装中文分词工具IK
1.进入ES目录并执行安装(注意版本号改成你需要的版本) cd /usr/share/elasticsearch ./bin/elasticsearch-plugin install https:// ...
- OI生涯回忆录(一)
OI生涯彻底结束了(难道不是早就结束了),有些东西不写可能就忘了,还是记录一下一些回忆叭.比较墨迹所以可能就连载了. (一)高一开学到NOIP2016 最开始就是觉得信息(计算机)竞赛可能会很有意思就 ...