使用RestTemplate:报错Could not extract response: no suitable HttpMessageConverter found for response typ

项目中需要调用微信接口获取access_token等一系列和微信接口相关的操作,我使用了Spring自带的RestTemplate类来发送Get或Post请求,直接在Spring配置文件中依赖注入


使用的时候直接

@Resource
private RestTemplate restTemplate;

结果在调用微信接口的时候出了异常

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; 
nested exception is org.springframework.web.client.RestClientException:
Could not extract response: no suitable HttpMessageConverter found for response type [] and content type [text/plain;charset=UTF-8]] with root cause org.springframework.web.client.RestClientException:
Could not extract response: no suitable HttpMessageConverter found for response type [] and content type [text/plain;charset=UTF-8]

原因:微信接口文档虽说返回的是 Json 数据,但是同时返回的 Header 里面的 Content-Type 值却是 text/plain 。

最终结果就是导致 RestTemplate 把数据从 HttpResponse 转换成 Object 的时候,找不到合适的 HttpMessageConverter 来转换。

在报错信息中知道,不支持[text/plain;charset=UTF-8]类型,需要继承 MappingJackson2HttpMessageConverter 并在构造过程中设置其支持的 MediaType 类型:

public class WxMappingJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter {
    public WxMappingJackson2HttpMessageConverter(){
        List<MediaType> mediaTypes = new ArrayList<>();
        mediaTypes.add(MediaType.TEXT_PLAIN);
        mediaTypes.add(MediaType.TEXT_HTML); 
        setSupportedMediaTypes(mediaTypes);
    }
}
public class RestTemplateUtil {
    public static RestTemplate getInstance() {
    	RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().add(new WxMappingJackson2HttpMessageConverter());
        return restTemplate;
    }
}

参考:很厉害的作者https://blog.csdn.net/kinginblue/article/details/52706155

你可能感兴趣的:(java)