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 就可以了

经验分享 程序员 微信小程序 职场和发展