java使用restTemplate报错:
org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class java.lang.Object] and content type [text/plain;charset=utf-8]
报错的代码:
ParameterizedTypeReference<Map<String,Object>> reference = new ParameterizedTypeReference<Map<String,Object>>(){};
ResponseEntity<Map<String,Object>> result = restTemplate.exchange(url,HttpMethod.GET,null,reference);
捕捉到的异常提示,因为没有响应类型Object和content type [text/plain;charset=utf-8]匹配的HttpMessageConverter,所以无法提取响应,看代码是用Map<String,Object>来接收的响应,而[text/plain;charset=utf-8]是纯文本,因此将我们的接收类型换为String即可,如果有需要可以之后再手动转换为JSON。更正后代码:
import com.alibaba.fastjson.JSONObject;
import org.springframework.core.ParameterizedTypeReference;
ParameterizedTypeReference<String> reference = new ParameterizedTypeReference<String>(){};
ResponseEntity<String> result = restTemplate.exchange(url, HttpMethod.GET,null,reference);
String obj = result.getBody();
JSONObject jsonObject = JSONObject.parseObject(responseBody);
响应正常接收:
“Message”: “deviceNo {deviceNo} not exist!”, “Content”: “”, “Success”: false
|