从网上找的方法:

方法一:

        //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版本)的更多相关文章

  1. Spring RestTemplate 中文乱码问题

    1.原因 由于RestTemplate的默认构造方法初始化的StringHttpMessageConverter的默认字符集是ISO-8859-1,所以导致RestTemplate请求的响应内容会出现 ...

  2. zabbix安装中文语言包及中文乱码的解决(zabbix5.0)

    一,zabbix不能配置中文界面的问题: 1, zabbix5.0 系统安装后,web界面不能选择使用中文 系统提示: You are not able to choose some of the l ...

  3. SpringBoot之整合Redis分析和实现-基于Spring Boot2.0.2版本

    背景介绍 公司最近的新项目在进行技术框架升级,基于的Spring Boot的版本是2.0.2,整合Redis数据库.网上基于2.X版本的整个Redis少之又少,中间踩了不少坑,特此把整合过程记录,以供 ...

  4. Spring Boot2.0以上版本EmbeddedServletContainerCustomizer被WebServerFactoryCustomizer替代

    在Spring Boot2.0以上配置嵌入式Servlet容器时EmbeddedServletContainerCustomizer类不存在,经网络查询发现被WebServerFactoryCusto ...

  5. RestTemplate中文乱码问题

    使用RestTemplate传输带有图片的表单时,需要对表单中的中文参数进行URL编码, eg :URLDecoder.decode(name);               // 使用默认的解码   ...

  6. RestTemplate 中文乱码

    @Configuration public class RestTemplateWithoutLoadBalance { @Bean("normalRestTemplate") p ...

  7. RestTemplate 中文乱码解决

    @Bean public RestTemplate restTemplate() { RestTemplate restTemplate = new RestTemplate(); restTempl ...

  8. Spring 3.0以后版本的定时任务

    自主开发的定时任务工具,spring task,可以将它比作一个轻量级的Quartz,而且使用起来很简单,除spring相关的包外不需要额外的包,而且支持注解和配置文件两种 <beans xml ...

  9. nodejs部署智能合约的方法-web3 0.20版本

    参考:https://www.jianshu.com/p/7e541cd67be2 部署智能合约的方法有很多,比如使用truffle框架,使用remix-ide等,在这里的部署方法是使用nodejs一 ...

随机推荐

  1. [洛谷P4705]玩游戏

    题目大意:对于每个$k\in[1,t]$,求:$$\dfrac{\sum\limits_{i=1}^n\sum\limits_{j=1}^m(a_i+b_j)^k}{nm}$$$n,m,t\leqsl ...

  2. 团体程序设计天梯赛 L3-016. 二叉搜索树的结构

    #include <cstdio> #include <cstdlib> #include <string.h> #include <math.h> # ...

  3. WARNING: pgstat wait timeout

      在ELK的邮件报警中,发现了一个 WARNING: pgstat wait timeout 的报错信息,看字面意思是pgstat有关操作等待超时.   通过google查询,发现在pg的邮件列表中 ...

  4. 字典的setdefault() 和get()方法比较

    Python 字典 setdefault() 函数 和get() 类似: 如果键存在字典中,返回其value值 如果键不存在字典中,创建键值对.完后,返回值为默认值. 话不多说,上栗子: setdef ...

  5. POJ - 2976 Dropping tests && 0/1 分数规划

    POJ - 2976 Dropping tests 你有 \(n\) 次考试成绩, 定义考试平均成绩为 \[\frac{\sum_{i = 1}^{n} a_{i}}{\sum_{i = 1}^{n} ...

  6. 科学计算三维可视化---Mayavi入门(Mayavi库的基本元素和绘图实例)

    一:Mayavi库的基本元素 .处理图形可视化和图形操作的mlab模块 .操作管线对象,窗口对象的api (一)mlab模块 (二)mayavi的api 二:快速绘图实例 (一)mlab.mesh的使 ...

  7. c++虚函数&重写

    虚函数是C++中实现多态的一种方法,父类A的一个函数声明为虚函数,在子类B中覆盖定义之后,当在调用的时候使用A*a=new B(),此时调用对应的那个虚函数的名字,则会执行B中的函数.当父类中没有定义 ...

  8. linux scp上传文件到其他机器上

    scp是secure copy的简写,用于在Linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝不能跨服务器,而且scp传输是加密的.可能会稍微影响一下速度.当你服务器 ...

  9. linux 系统下IntelliJ IDEA的安装及使用

    由于刚刚进入研究生阶段,通过几个月对大数据的学习,从java到hadoop,再到scala到spark.在这我写一下我在ubuntu系统下intelliJ IDEA的安装和配置.首先我的ubuntu系 ...

  10. Windows系统环境下Solr之Java实战(二)配置从MySQL数据库批量导入索引

    1.将D:\JavaWeb\Solr\solr-6.2.0\dist下面的solr-dataimporthandler-6.2.0.jar和solr-dataimporthandler-extras- ...