使用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,存的時候值會以數組的方式保存,

比如我們在做表單提交的時候,表單數據中可能存在鍵相同值不同的情況,可以用這種方法存值。

 

 
       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM