UnknownContentTypeException 解决办法

原文链接: UnknownContentTypeException 解决办法

异常信息:

 org.springframework.web.client.UnknownContentTypeException: Could not extract response: no suitable HttpMessageConverter found for response type 
[class com.byd.iot.model.rest.ApiResult] and content type [text/html]

原因:

RestTemplate的默认处理响应类型为application/json,但接口返回数据格式为text/html,RestTemplate无法转换信息报错:UnknownContentTypeException

ResponseEntity中返回包含了响应url接口中的json数据,而此处是未找到text/html类型

解决方案:用byte[]接收

//发送http请求,返回数据格式为text/html用byte[]接收
        ResponseEntity<byte[]> responseEntity = restTemplate.postForEntity(url, httpEntity, byte[].class);
        //获取响应体中的内容
        byte[] responseEntityBody = responseEntity.getBody();
        //将byte[]转为JSON格式的字符串
        String json = new String(responseEntityBody);
posted @ 2022-11-29 21:41  进击的小蔡鸟  阅读(2307)  评论(0编辑  收藏  举报