如何解决使用restTemplate进行feign调用new HttpEntity<>报错问题
restTemplate进行feign调用new HttpEntity<>报错
问题背景
今天才知道restTemplate可以直接调用feign,高级用法呀,但使用restTemplate进行feign调用new HttpEntity<>报错了标红了

导入的错包为:
import org.apache.http.HttpEntity;
HttpEntity<>标红解决方案
1 原来是因为引错了包,在标红处使用快捷键alt+enter,选第二个改变类型


更改新包为:
import org.springframework.http.ResponseEntity;
心得
不同依赖导致的问题,要多注意
resttemplate调用HttpEntity 产生报错
项目场景
resttemplate调用HttpEntity 产生报错
传输过程
问题描述
org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [[Lorg.apache.commons.httpclient.NameValuePair;] and content type [application/x-www-form-urlencoded]
原因分析
HashMap<String, String> map = new HashMap<>();
map.put("xmlData", "xmlDataInfo");
//上面的map直接塞进request请求里会报错
/**
* org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter
* found for request type [[Lorg.apache.commons.httpclient.NameValuePair;] and content type [application/x-www-form-urlencoded
*/
//应该把map换成NameValuePair[] data = { new NameValuePair("xmlData",string) };
NameValuePair[] data = { new NameValuePair("xmlData",string) };
HttpEntity<String> httpEntity = new HttpEntity(data, headers);
//这样就可以了解决方案
应该把hashmap 换成 MultiValueMap 就可以了
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
Springboot在有参构造方法类中使用@Value注解取值
这篇文章主要介绍了Springboot在有参构造方法类中使用@Value注解取值,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下2020-06-06


最新评论