标签:
/* * 初始化RestTemplate,RestTemplate会默认添加HttpMessageConverter * 添加的StringHttpMessageConverter非UTF-8 所以先要移除原有的StringHttpMessageConverter, * 再添加一个字符集为UTF-8的StringHttpMessageConvert */ private void reInitMessageConverter(RestTemplate restTemplate) { List<HttpMessageConverter<?>> converterList = restTemplate.getMessageConverters(); HttpMessageConverter<?> converterTarget = null; for (HttpMessageConverter<?> item : converterList) { if (item.getClass() == StringHttpMessageConverter.class) { converterTarget = item; break; } } if (converterTarget != null) { converterList.remove(converterTarget); } HttpMessageConverter<?> converter = new StringHttpMessageConverter(StandardCharsets.UTF_8); converterList.add(converter); }
标签: resttemplate 中文乱码 方法 | 发表时间:2016-02-01 15:51 | 作者:yangzelin_job09
出处:http://www.iteye.com
Spring4.2.2.RELEASE RestTemplate 中文乱码解决方法
RestTempalate 字符串类型默认StringHttpMessageConverter做转码,而StringHttpMessageConverter的默认编码集是ISO8859-1,本工程编码是UTF-8 故发送到服务器端会存在中文乱码
第一种:把restTempalate 中 StringHttpMessageConverter 默认编码设置为UTF-8
具体:使用 restTempalate 构造函数 public RestTemplate(List<HttpMessageConverter<?>> messageConverters) 传入 StringHttpMessageConverter 默认编码集是UTF-8 的转换器
在 配置文件中配置:
<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请求头中设置 Content-Type: application/x-www-form-urlencoded; charset=UTF-8 修改默认编码
HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType("application/x-www-form-urlencoded; charset=UTF-8");
headers.setContentType(type);
System.out.println(type);
HttpEntity<String> requestEntity = new HttpEntity<String>(PostStrUtils.getPostStrFromMap(paramMap), headers);
String msg = restTemplate.postForObject(url,requestEntity, String.class);
public String sendToRest(String url, Map<String, String> paramMap) {
logger.debug("====================start======================");
logger.debug("调用接口-" + restUrl + url + " 入参:" + paramMap);
/**
* restTempalate 字符串类型默认StringHttpMessageConverter做转码,而StringHttpMessageConverter的默认编码集是ISO8859-1,本工程编码是UTF-8 故发送到服务器端会存在中文乱码
* 解决方法有两种:
* 第一种:把restTempalate 中 StringHttpMessageConverter 默认编码设置为UTF-8
* 具体:使用 restTempalate 构造函数 public RestTemplate(List<HttpMessageConverter<?>> messageConverters) 传入 StringHttpMessageConverter 默认编码集是UTF-8 的转换器
* 在 配置文件中配置:
* <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请求头中设置 Content-Type: application/x-www-form-urlencoded; charset=UTF-8 修改默认编码
*
*/
//存在中文乱问题
// String msg = restTemplate.postForObject(restUrl + url,
// PostStrUtils.getPostStrFromMap(paramMap), String.class);
//
HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType("application/x-www-form-urlencoded; charset=UTF-8");
headers.setContentType(type);
System.out.println(type);
HttpEntity<String> requestEntity = new HttpEntity<String>(PostStrUtils.getPostStrFromMap(paramMap), headers);
String msg = restTemplate.postForObject(url,requestEntity, String.class);
logger.debug("调用接口-" + restUrl + url + " 返回:" + msg);
logger.debug("====================end========================");
return msg;
}
标签:
原文地址:http://www.cnblogs.com/wangyang108/p/5754186.html