Quantcast
Channel: IT社区推荐资讯 - ITIndex.net
Viewing all articles
Browse latest Browse all 11813

RestTemplate 中文乱码解决方法

$
0
0
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;
	}




已有 0人发表留言,猛击->> 这里<<-参与讨论


ITeye推荐




Viewing all articles
Browse latest Browse all 11813

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>