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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南