项目中需要调用微信接口获取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相关推荐

  1. RestTemplate请求Could not extract response: no suitable HttpMessageConverter found for response type..

    使用 Spring Boot 写项目,需要用到微信接口获取用户信息. 在 Jessey 和 Spring RestTemplate 两个 Rest 客户端中,想到尽量不引入更多的东西,然后就选择了 S ...

  2. 小小涉及OpenFeign原理:Could not extract response: no suitable HttpMessageConverter found for response type

    一.问题解释(想看总结的去最下面) org.springframework.web.client.UnknownContentTypeException: Could not extract resp ...

  3. 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 ...

  4. Could not extract response: no suitable HttpMessageConverter found for response type ***

    用RestTemplate.getForObject()获取URL的JSON时,可能会遇到如题所示的报错信息. 我的问题在于:在非MVC的Project中使用RestTemplate. 简单的说,me ...

  5. 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. ...

  6. openFeig远程调用报错Could not extract response: no suitable HttpMessageConverter found for response type

    解决方案:在调用服务和被调用的服务上都加上一个配置类 @Configuration public class jsonConfig {@Beanpublic HttpMessageConverters ...

  7. restTemplate http请求报错:no suitable HttpMessageConverter found for response type and content type

    报错信息: org.springframework.web.client.UnknownContentTypeException: Could not extract response: no sui ...

  8. Could not extract response: no suitable HttpMessageConverter found for content type [text/html]

    目录 报错信息 源码分析 解决方法 修改 mappingJackson2HttpMessageConverter 配置 继承 mappingJackson2HttpMessageConverter 实 ...

  9. 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 ...

最新文章

  1. javabean属性的类型选择包装类还是基本数据类型
  2. 作为数据科学家,我都有哪些弱点
  3. 为什么 GROUP BY 之后不能直接引用原表中的列?
  4. [java]常用类型转化
  5. rxjs里withLatestFrom operators的用法
  6. SAP CRM中间件sales status调试
  7. Android之android.os.NetworkOnMainThreadException异常
  8. 【BZOJ1069】【SCOI2007】—最大土地面积(凸包+旋转卡壳)
  9. oracle层次化查询
  10. 2020年2月数据库流行度排行:冬日虽然寒冷,春光必定灿烂
  11. Python语言编程规范与优化建议
  12. Android编译过程详解(三)
  13. windows本地安装PLSQL
  14. 梦三花重金修改服务器,2021年3月31日维护公告:新门派花果山
  15. matlab设置柱状图空白,MATLAB画柱状图时形状填充及颜色改变问题
  16. VMWare安装报错:此安装程序要求您重新启动系统以完成 Microsoft VC Redistributable安装,然后重新运行该安装程序。
  17. UVM入门与进阶学习笔记1——UVM概述、类库地图、工厂机制、覆盖方法
  18. Leetcode PHP题解--D38 463. Island Perimeter
  19. Java设计模式的作用
  20. linux运行魔力宝贝,魔力宝贝私服架设(仅供朋友交流学习 绝无商业用途 如有雷同纯熟巧合)...

热门文章

  1. 力扣刷题-python-动态规划-1 (动态规划、01背包问题、完全背包问题)
  2. Unity利用单反相机拍照、录制视频
  3. 基于8086单片机的PWM调光(带汇编)
  4. uniapp之微信小程序开发——根据主题色动态修改svg的颜色
  5. 线程进程是怎样使用多核的
  6. R语言 一元正态分布参数最大似然估计
  7. 简单字符驱动笔记(朱有鹏)
  8. 云主机上手教程:轻量应用服务器体验
  9. 一位计算机专业硕士毕业生的求职经历和感想[转载]
  10. layui-富文本编辑器