RestTemplate HttpMessageConverter报错的解决方案no suitable HttpMessageConverter
错误
no suitable HttpMessageConverter found for response type and content type [text/html;charset=UTF-8]
这边调用的时候使用了RestTemplate
使用过程
RestTemplate restTemplate = new RestTemplate();
String payUrlFinal = "http://127.0.0.1/pay?orderId=1";
PayResponse payResponse = restTemplate.getForObject(payUrlFinal, PayResponse.class);
下面是我请求的路径
@GetMapping("/pay")
@ResponseBody
public PayResponse pay(@RequestParam("orderId") String orderId){
//1.查询订单
OrderDTO orderDTO = orderService.findOne(orderId);
if(Objects.isNull(orderDTO)){
//订单不存在
throw new SellException(ResultEnum.ORDER_NOT_EXIST);
}
//发起支付
PayResponse payResponse = payService.create(orderDTO);
return payResponse;
}
调用之后程序报以下错误
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 [class com.lly835.bestpay.model.PayResponse] and content type [text/html;charset=UTF-8]] with root cause
org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.lly835.bestpay.model.PayResponse] and content type [text/html;charset=UTF-8]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:109)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:655)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:287)
at com.imooc.controller.PayController.create(PayController.java:107)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
解决方案:
我们继承 MappingJackson2HttpMessageConverter 并在构造过程中设置其支持的 MediaType 类型即可:
public class WxMappingJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter {
public WxMappingJackson2HttpMessageConverter(){
List<MediaType> mediaTypes = new ArrayList<>();
mediaTypes.add(MediaType.TEXT_PLAIN);
mediaTypes.add(MediaType.TEXT_HTML); //加入text/html类型的支持
setSupportedMediaTypes(mediaTypes);// tag6
}
}
然后把这个 WxMappingJackson2HttpMessageConverter 追加到 RestTemplate 的 messageConverters 消息转换链中去:
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new WxMappingJackson2HttpMessageConverter());
String payUrlFinal = "http://127.0.0.1/pay?orderId=1";
PayResponse payResponse = restTemplate.getForObject(payUrlFinal, PayResponse.class);
具体原因分析可参照下面的地址:
http://blog.csdn.net/kinginblue/article/details/52706155
参考
原文:https://blog.csdn.net/u011768325/article/details/77097655
RestTemplate HttpMessageConverter报错的解决方案no suitable HttpMessageConverter的更多相关文章
- 关于Entity Framework中的Attached报错相关解决方案的总结
关于Entity Framework中的Attached报错的问题,我这里分为以下几种类型,每种类型我都给出相应的解决方案,希望能给大家带来一些的帮助,当然作为读者的您如果觉得有不同的意见或更好的方法 ...
- 新手常见的python报错及解决方案
此篇文章整理新手编写代码常见的一些错误,有些错误是粗心的错误,但对于新手而已,会折腾很长时间才搞定,所以在此总结下我遇到的一些问题.希望帮助到刚入门的朋友们.后续会不断补充. 目录 1.NameErr ...
- Mysql only_full_group_by以及其他关于sql_mode原因报错详细解决方案
Mysql only_full_group_by以及其他关于sql_mode原因报错详细解决方案 网上太多相关资料,但是抄袭严重,有的讲的也是之言片语的,根本不连贯(可能知道的人确实不想多说) 我总共 ...
- Win7下nginx默认80端口被System占用,造成nginx启动报错的解决方案
Win7下nginx默认80端口被System占用,造成nginx启动报错的解决方案 在win7 32位旗舰版下,启动1.0.8版本nginx,显示如下错误: [plain] 2012/04/0 ...
- Mac上PyCharm运行多进程报错的解决方案
Mac上PyCharm运行多进程报错的解决方案 运行时报错 may have been in progress in another thread when fork() was called. We ...
- 安装Redis-cluster-gem install redis报错的解决方案
错误描述: [root@eshop-cache01 local]# gem install redis ERROR: Loading command: install (LoadError) cann ...
- ELK报错及解决方案
ELK报错及解决方案 1.jdk版本问题 报错如下: future versions of Elasticsearch will require Java 11; your Java version ...
- 在eclipse中引入jquery.js文件报错的解决方案
从官方下载的jquery.js在myeclipse始终用个大大的红叉,看着很不爽,如何解决呢:jquery.js在myeclipse中报错:jquery.js -> 鼠标右键 -> MyE ...
- 利用Maven管理工程项目本地启动报错及解决方案
目前利用Maven工具来构建自己的项目已比较常见.今天主要不是介绍Maven工具,而是当你本地启动这样的服务时,如果遇到报错,该如何解决?下面只是参考的解决方案,具体的解法还是得看log的信息. 1. ...
随机推荐
- Linux下制作和使用静态库和动态库
概述 Linux操作系统支持的函数库分为静态库和动态库,动态库又称共享库.linux系统有几个重要的目录存放相应的函数库,如/lib /usr/lib. 静态函数库: 这类库的名字一般是libxxx. ...
- 第十七篇 Linux下常用命令汇总
- 计算几何-Dot-Vector
This article is made by Jason-Cow.Welcome to reprint.But please post the article's address. 看了书,然后码 ...
- HBO《硅谷》中的二进制码
先反思一下……这两天感觉除了coding,没有很好地去学习专业课.心神不定 于是,就想看下硅谷,来提升一下自己的coding执行力…… 然后,我就在看剧的时候,看到了这么一张图:‘ 然后嘛…… 我就想 ...
- yum源文件
yum源文件 2017年11月22日 11:59:36 cakincqm 阅读数 1052更多 分类专栏: Linux 版权声明:本文为博主原创文章,遵循CC 4.0 by-sa版权协议,转载请附 ...
- CentOS: 网络连接故障排除
yum不能正常动作,惯性认为是需要替换BaseURL,结果后来才发现是Gateway不知什么时候被错误设定了,memo如下. 现象表现于yum不能正常动作,确认ping的操作,发现是不能解析DNS [ ...
- css常见问题汇总
1. 如果我想显示两行文字第二行超出部分‘...’? 限制在一个块元素显示的文本的行数. -webkit-line-clamp 是一个 不规范的属性(unsupported WebKit proper ...
- java反射--超级好用的方法
1.根据方法名,获取类的对应的方法 Method changeSessionIdMethod = ReflectionUtils.findMethod(HttpServletRequest.class ...
- 一张linux光盘查看是哪个版本号的方法
在此查看版本号,方法如下:打开光盘,查找rpm包中的release,就是版本号.
- wordpress 支持上传中文名称文件
添加文章难免要传个图.文件啥的,可是呢,上传中文名称的文件竟然不行,找了半天,中文乱码,脑残了,竟然忘了这个事,哎 修改其实很简单,只需要两步 1./wp-admin/includes/file.ph ...