如何解決使用restTemplate進(jìn)行feign調(diào)用new HttpEntity<>報錯問題
restTemplate進(jìn)行feign調(diào)用new HttpEntity<>報錯
問題背景
今天才知道restTemplate可以直接調(diào)用feign,高級用法呀,但使用restTemplate進(jìn)行feign調(diào)用new HttpEntity<>報錯了標(biāo)紅了
導(dǎo)入的錯包為:
import org.apache.http.HttpEntity;
HttpEntity<>標(biāo)紅解決方案
1 原來是因?yàn)橐e了包,在標(biāo)紅處使用快捷鍵alt+enter,選第二個改變類型
更改新包為:
import org.springframework.http.ResponseEntity;
心得
不同依賴導(dǎo)致的問題,要多注意
resttemplate調(diào)用HttpEntity 產(chǎn)生報錯
項目場景
resttemplate調(diào)用HttpEntity 產(chǎn)生報錯
傳輸過程
問題描述
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直接塞進(jìn)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 */ //應(yīng)該把map換成NameValuePair[] data = { new NameValuePair("xmlData",string) }; NameValuePair[] data = { new NameValuePair("xmlData",string) }; HttpEntity<String> httpEntity = new HttpEntity(data, headers); //這樣就可以了
解決方案
應(yīng)該把hashmap 換成 MultiValueMap 就可以了
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot在有參構(gòu)造方法類中使用@Value注解取值
這篇文章主要介紹了Springboot在有參構(gòu)造方法類中使用@Value注解取值,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06SpringBoot接收接口入?yún)⒌姆绞叫〗Y(jié)
這篇文章主要給大家介紹了SpringBoot接收接口入?yún)⒌膸追N方式,我們從調(diào)用方的視角去看待這個問題,對調(diào)用方來說,它在調(diào)用接口時有好幾種傳參方式,下面,將會依次對這幾種參數(shù)方式進(jìn)行講解和代碼示例,需要的朋友可以參考下2024-01-01java實(shí)現(xiàn)文件導(dǎo)入導(dǎo)出
這篇文章主要介紹了java實(shí)現(xiàn)文件導(dǎo)入導(dǎo)出的方法和具體示例代碼,非常的簡單實(shí)用,有需要的小伙伴可以參考下2016-04-04Spring Boot 開發(fā)環(huán)境熱部署詳細(xì)教程
這篇文章主要介紹了Spring Boot 開發(fā)環(huán)境熱部署,本文給大家介紹了Spring Boot 開發(fā)環(huán)境熱部署的原理及快速配置方法,通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06