RestTemplate中文乱码问题(spring-web3.0.7版本)
从网上找的方法:
方法一:
//resttemplate乱码问题
//3.1.X以上版本使用
// restTemplate.getMessageConverters().add(0, StringHttpMessageConverter.DEFAULT_CHARSET);
// 3.0版本
List<HttpMessageConverter<?>> converterList=restTemplate.getMessageConverters();
HttpMessageConverter<?> converter = new StringHttpMessageConverter();
converterList.add(0, converter);
restTemplate.setMessageConverters(converterList); HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
headers.setContentType(type);
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
参考:
https://blog.csdn.net/kinbridge/article/details/73477731
方法二:
xml配置
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<constructor-arg index="0">
<list>
<bean id="byteArrayHttpMessageConverter"
class="org.springframework.http.converter.ByteArrayHttpMessageConverter"></bean>
<bean id="stringHttpMessageConverter"
class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"></constructor-arg>
</bean>
<bean id="resourceHttpMessageConverter"
class="org.springframework.http.converter.ResourceHttpMessageConverter"></bean>
<bean id="sourceHttpMessageConverter"
class="org.springframework.http.converter.xml.SourceHttpMessageConverter"></bean>
<bean id="allEncompassingFormHttpMessageConverter"
class="org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter"></bean>
<bean id="jaxb2RootElementHttpMessageConverter"
class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"></bean>
<bean id="mappingJackson2HttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
</list>
</constructor-arg>
</bean>
参考:
http://yangzelin-job09.iteye.com/blog/2275367
方法三:
xml配置
<bean id="ky.requestFactory" class="org.springframework.http.client.SimpleClientHttpRequestFactory">
<property name="readTimeout" value="10000"/>
<property name="connectTimeout" value="5000"/>
</bean> <bean id="simpleRestTemplate" class="org.springframework.web.client.RestTemplate">
<constructor-arg ref="ky.requestFactory"/>
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.FormHttpMessageConverter"/>
<bean class="org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter"/>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
参考:
https://blog.csdn.net/qq_32193151/article/details/72902898
以上都试过,可能使我们spring的版本太低,具体也不大清楚,还出现乱码问题,有时候最简单的也是最有效的,我直接用new String()的方式,解决了!!!
终极解决方法:
查看spring源码中org.springframework.http.converter.StringHttpMessageConverter类中,默认编码格式为默认为ISO-8859-1。
在java代码中直接转换。
//restTemplate默认为iso8859-1,需要转换编码格式
try {
body = new String(body.getBytes("iso8859-1"),"utf-8");
} catch (UnsupportedEncodingException e1) {
log.error("iso8859-1编码格式转换utf-8错误");
e1.printStackTrace();
}
原来的:éæ乱码解决
RestTemplate中文乱码问题(spring-web3.0.7版本)的更多相关文章
- Spring RestTemplate 中文乱码问题
1.原因 由于RestTemplate的默认构造方法初始化的StringHttpMessageConverter的默认字符集是ISO-8859-1,所以导致RestTemplate请求的响应内容会出现 ...
- zabbix安装中文语言包及中文乱码的解决(zabbix5.0)
一,zabbix不能配置中文界面的问题: 1, zabbix5.0 系统安装后,web界面不能选择使用中文 系统提示: You are not able to choose some of the l ...
- SpringBoot之整合Redis分析和实现-基于Spring Boot2.0.2版本
背景介绍 公司最近的新项目在进行技术框架升级,基于的Spring Boot的版本是2.0.2,整合Redis数据库.网上基于2.X版本的整个Redis少之又少,中间踩了不少坑,特此把整合过程记录,以供 ...
- Spring Boot2.0以上版本EmbeddedServletContainerCustomizer被WebServerFactoryCustomizer替代
在Spring Boot2.0以上配置嵌入式Servlet容器时EmbeddedServletContainerCustomizer类不存在,经网络查询发现被WebServerFactoryCusto ...
- RestTemplate中文乱码问题
使用RestTemplate传输带有图片的表单时,需要对表单中的中文参数进行URL编码, eg :URLDecoder.decode(name); // 使用默认的解码 ...
- RestTemplate 中文乱码
@Configuration public class RestTemplateWithoutLoadBalance { @Bean("normalRestTemplate") p ...
- RestTemplate 中文乱码解决
@Bean public RestTemplate restTemplate() { RestTemplate restTemplate = new RestTemplate(); restTempl ...
- Spring 3.0以后版本的定时任务
自主开发的定时任务工具,spring task,可以将它比作一个轻量级的Quartz,而且使用起来很简单,除spring相关的包外不需要额外的包,而且支持注解和配置文件两种 <beans xml ...
- nodejs部署智能合约的方法-web3 0.20版本
参考:https://www.jianshu.com/p/7e541cd67be2 部署智能合约的方法有很多,比如使用truffle框架,使用remix-ide等,在这里的部署方法是使用nodejs一 ...
随机推荐
- 【刷题】BZOJ 3514 Codechef MARCH14 GERALD07加强版
Description N个点M条边的无向图,询问保留图中编号在[l,r]的边的时候图中的联通块个数. Input 第一行四个整数N.M.K.type,代表点数.边数.询问数以及询问是否加密. 接下来 ...
- 【Learning】积性函数前缀和——洲阁筛(min_25写法)
问题描述 洲阁筛解决的问题主要是\(n\)范围较大的积性函数前缀和. 已知一积性函数\(f(i)\),求\(\sum_{i=1}^nf(i)\). \(n\leq10^{12}\). 求解方法 如 ...
- 【poj3623】 Best Cow Line, Gold
http://poj.org/problem?id=3623 (题目链接) 题意 给出一个字符串,每次可以取首或尾接到一个新的字符串后面,求构出的字典序最小的新字符串. Solution 首先可以发现 ...
- Android获取程序路径 (/data/data/appname)
Android获取文件夹路径 /data/data/ http://www.2cto.com/kf/201301/186614.html String printTxtPath = getApplic ...
- 团体程序设计天梯赛-练习集 L1-031. 到底是不是太胖了
比较两个实型的数: 若两者相等,也许用a>/b会出错... 我又想到了codeforces有很多这样的坑... #include <stdio.h> #include <std ...
- CH3101 阶乘分解
题目链接 分解\(n!\)的质因数,输出相应的\(p_i\)和\(c_i\). 其中\(1\leq n\leq 10^6\). 考虑每一个质因子 \(p\) 在 \(n!\) 中出现的次数.显然, ...
- 时间序列HW
https://www.cnblogs.com/sylvanas2012/p/4328861.html写得特别好,推荐阅读 Holt-Winters: 三阶指数平滑 Holt-Winters的思想是 ...
- P4887 第十四分块(前体) 莫队
题意: 给你一个序列,每次询问l,r问多少个a[i]^a[j]有k个1,k固定. 序列长度1e5,a[i]<=2^14 时限1s,空间40M 题解: 个人其实开始没什么思路,看了题解也好久,题解 ...
- java io读写文件
java io读写文件相关阅读:http://www.cnblogs.com/wing011203/archive/2013/05/03/3056535.html public class DemoI ...
- javascript精雕细琢(四):认亲大戏——通过console.log彻底搞清this
目录 引言 代码在前 1.function下的this 2.箭头函数下的this 结语 引言 JS中的this指向一直是个老生常谈,但是新手又容易晕的地方.我在网上浏览了很多帖子,但是发 ...