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,存的时候值会以数组的方式保存,

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

    
    
  • 相关阅读:
    团队冲刺第五天
    单词统计包括其续
    团队冲刺第四天
    团队冲刺第三天
    团队冲刺第二天
    团队冲刺第一天
    软件工程第九周总结
    软件工程第八周总结
    《大道至简》阅读笔记二
    《大道至简》阅读笔记一
  • 原文地址:https://www.cnblogs.com/lvhouhou/p/13386478.html
Copyright © 2011-2022 走看看