RestTemplate配置类Error while extracting ...content type [application/json;charset=utf-16];

熊孩纸 阅读:3496 2021-04-01 11:04:50 评论:0

1.创建restTemplate对象
原始的发现有问题:

@Configuration 
public class RestTemplateConfig { 
	@Bean 
	public RestTemplate restTemplate() { 
		RestTemplate restTemplate = new RestTemplate(); 
		return restTemplate; 
	} 
} 
Error while extracting response for type [class com.alibaba.fastjson.JSONObject] and content type [application/json;charset=utf-16]; nested exception is java.net.SocketTimeoutException: Read timed out 

改成:

@Configuration 
public class RestTemplateConfig { 
	@Autowired 
	RestTemplateBuilder builder; 
 
	@Bean 
	public RestTemplate restTemplate() { 
		RestTemplate restTemplate = builder.build(); 
		return restTemplate; 
	} 
} 

2.测试方法

	String reqLogin = "{\"loginName\":\"zhangsan\",\"pwd\":\"123456\""}"; 
				HttpHeaders headers = new HttpHeaders(); 
				headers.add("Content-Type", "application/json;charset=utf-8"); 
				HttpEntity<String> httpEntity = new HttpEntity<String>(reqLogin, headers); 
				JSONObject resp = restTemplate.postForObject("http://XXX", httpEntity, JSONObject.class); 
标签:json
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号