使用RestTemplate:报错Could not extract response: no suitable HttpMessageConverter found for response typ
项目中需要调用微信接口获取access_token等一系列和微信接口相关的操作,我使用了Spring自带的RestTemplate类来发送Get或Post请求,直接在Spring配置文件中依赖注入
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate"/>
使用的时候直接
@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
使用RestTemplate:报错Could not extract response: no suitable HttpMessageConverter found for response typ相关推荐
- RestTemplate请求Could not extract response: no suitable HttpMessageConverter found for response type..
使用 Spring Boot 写项目,需要用到微信接口获取用户信息. 在 Jessey 和 Spring RestTemplate 两个 Rest 客户端中,想到尽量不引入更多的东西,然后就选择了 S ...
- 小小涉及OpenFeign原理:Could not extract response: no suitable HttpMessageConverter found for response type
一.问题解释(想看总结的去最下面) org.springframework.web.client.UnknownContentTypeException: Could not extract resp ...
- Could not extract response: no suitable HttpMessageConverter found for response type [class com.exam
报错信息:Could not extract response: no suitable HttpMessageConverter found for response type [class com ...
- Could not extract response: no suitable HttpMessageConverter found for response type ***
用RestTemplate.getForObject()获取URL的JSON时,可能会遇到如题所示的报错信息. 我的问题在于:在非MVC的Project中使用RestTemplate. 简单的说,me ...
- Could not extract response: no suitable HttpMessageConverter found for response type [class java.lan
解决 Could not extract response: no suitable HttpMessageConverter found for response type [class java. ...
- openFeig远程调用报错Could not extract response: no suitable HttpMessageConverter found for response type
解决方案:在调用服务和被调用的服务上都加上一个配置类 @Configuration public class jsonConfig {@Beanpublic HttpMessageConverters ...
- restTemplate http请求报错:no suitable HttpMessageConverter found for response type and content type
报错信息: org.springframework.web.client.UnknownContentTypeException: Could not extract response: no sui ...
- Could not extract response: no suitable HttpMessageConverter found for content type [text/html]
目录 报错信息 源码分析 解决方法 修改 mappingJackson2HttpMessageConverter 配置 继承 mappingJackson2HttpMessageConverter 实 ...
- Could not extract response: no suitable HttpMessageConverter
版本:spring-cloud-openfeign-core-2.1.1.RELEASE.jar,spring-webmvc-5.1.14.RELEASE.jar,jetty-server-9.4.4 ...
最新文章
- javabean属性的类型选择包装类还是基本数据类型
- 作为数据科学家,我都有哪些弱点
- 为什么 GROUP BY 之后不能直接引用原表中的列?
- [java]常用类型转化
- rxjs里withLatestFrom operators的用法
- SAP CRM中间件sales status调试
- Android之android.os.NetworkOnMainThreadException异常
- 【BZOJ1069】【SCOI2007】—最大土地面积(凸包+旋转卡壳)
- oracle层次化查询
- 2020年2月数据库流行度排行:冬日虽然寒冷,春光必定灿烂
- Python语言编程规范与优化建议
- Android编译过程详解(三)
- windows本地安装PLSQL
- 梦三花重金修改服务器,2021年3月31日维护公告:新门派花果山
- matlab设置柱状图空白,MATLAB画柱状图时形状填充及颜色改变问题
- VMWare安装报错:此安装程序要求您重新启动系统以完成 Microsoft VC Redistributable安装,然后重新运行该安装程序。
- UVM入门与进阶学习笔记1——UVM概述、类库地图、工厂机制、覆盖方法
- Leetcode PHP题解--D38 463. Island Perimeter
- Java设计模式的作用
- linux运行魔力宝贝,魔力宝贝私服架设(仅供朋友交流学习 绝无商业用途 如有雷同纯熟巧合)...