使用restTemplate出现的异常

1.org.springframework.web.client.UnknownContentTypeException: Could not extract response: no suitable HttpMessageConverter found for response type [class zycf.cloud.bean.SafetyResponseType] and content type [text/html]

 

除了所有的答案之外,如果碰巧收到了text/html,而你期望别的东西(即application/json),则可能表明服务器端发生了错误(比如404)并且返回了错误页面而不是你的数据。

 

用postMan测试,果然是的,这个接口期望收到json数据,却收到了HTML页面

 

2.org.springframework.web.client.RestClientException: No HttpMessageConverter for java.util.HashMap and content type "multipart/form-data"

HashMap参数改为LinkedMultiValueMap
 MultiValueMap<String, String> params = new LinkedMultiValueMap<>();

        params.add("clientId",ZHEJIANG_CLIENT_ID );
        params.add("nonce", uuid);                      //随机数
        params.add("timestamp", dateString);             //时间戳
        params.add("signature", signature);           //签名


        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);

        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
        /**  发送请求*/
        RestTemplate restTemplate = new RestTemplate();
        SafetyResponseType safetyResponseType = restTemplate.postForObject(SAFETY_CERTIFICATE_URL, requestEntity, SafetyResponseType.class );

 采用这种方法反序列化的时候,如果json字符串中有相同的key,存的时候值会以数组的方式保存,

比如我们在做表单提交的时候,表单数据中可能存在键相同值不同的情况,可以用这种方法存值。

 


posted @ 2020-07-27 17:19  从来没有平凡的时刻  阅读(6470)  评论(1编辑  收藏  举报