zoukankan      html  css  js  c++  java
  • 使用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,存的时候值会以数组的方式保存,

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

    
    
  • 相关阅读:
    Vjios P1736 铺地毯【暴力,思维】
    Vijos P1116 一元三次方程求解【多解,暴力,二分】
    Vijos P1131 最小公倍数和最大公约数问题【暴力】
    Vijos P1786 质因数分解【暴力】
    C++课程设计类作业4
    析构函数的用法【简单理论讲解】
    C++课程设计类作业3
    最短路径Floyd算法【图文详解】
    HDU 1874 畅通工程续【Floyd算法实现】
    C++课程设计类作业2
  • 原文地址:https://www.cnblogs.com/lvhouhou/p/13386478.html
Copyright © 2011-2022 走看看