RestTemplate 中文乱码解决方法
- - 企业架构 - ITeye博客Spring4.2.2.RELEASE RestTemplate 中文乱码解决方法. RestTempalate 字符串类型默认StringHttpMessageConverter做转码,而StringHttpMessageConverter的默认编码集是ISO8859-1,本工程编码是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>
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; }