no suitable HttpMessageConverter found for response type [XXX]

2024-03-23 02:48

本文主要是介绍no suitable HttpMessageConverter found for response type [XXX],希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

1、背景

2、报错详情

3、代码定位

4、问题解决


1、背景

使用Spring的RestTemplate进行网络请求,RestTemplate把数据从 HttpResponse 转换成Object的时候找不到合适的HttpMessageConverter

2、报错详情

Could not extract response: no suitable HttpMessageConverter found for response type [XXX] and content type [text/html;charset=UTF-8]

3、代码定位

从org.springframework.web.client.RestTemplate#execute()开始往下走

定位到org.springframework.web.client.RestTemplate.ResponseEntityResponseExtractor#extractData()

public ResponseEntity<T> extractData(ClientHttpResponse response) throws IOException {if (this.delegate != null) {//核心:转换响应数据(HttpResponse 转换成Object)T body = this.delegate.extractData(response);return ResponseEntity.status(response.getRawStatusCode()).headers(response.getHeaders()).body(body);}else {return ResponseEntity.status(response.getRawStatusCode()).headers(response.getHeaders()).build();}
}

可以看到,收到response的Header里面的Content-Type值为text/html;charset=UTF-8

继续往下走,通用的http消息转换器匹配到自己的子类MappingJackson2HttpMessageConverter,但是可以看到的是supportedMediaTypes 只支持:application/json 的 MediaType。并不能转换text/html

未匹配到转换器,走到方法最后,就会抛出异常 

throw new UnknownContentTypeException(this.responseType, contentType,response.getRawStatusCode(), response.getStatusText(), response.getHeaders(),getResponseBody(response));

4、问题解决

需要一个转换器能处理头部 Content-Type 为 text/html 类型的 Json 返回值的话,就能把 Json 反序列化成我们要的对象。

所以需要自定义一个转换器,参照MappingJackson2HttpMessageConverter,继承AbstractJackson2HttpMessageConverter

public class XmMappingJackson2HttpMessageConverter extends AbstractJackson2HttpMessageConverter {public XmMappingJackson2HttpMessageConverter(ObjectMapper objectMapper) {super(objectMapper, MediaType.TEXT_HTML);}
}

可以看到,转换器列表增加了我们的XmMappingJackson2HttpMessageConverter

踩坑:别直接继承MappingJackson2HttpMessageConverter,然后去setSupportedMediaTypes,这样会把人本身覆盖掉

这篇关于no suitable HttpMessageConverter found for response type [XXX]的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/836891

相关文章

Java Response返回值的最佳处理方案

《JavaResponse返回值的最佳处理方案》在开发Web应用程序时,我们经常需要通过HTTP请求从服务器获取响应数据,这些数据可以是JSON、XML、甚至是文件,本篇文章将详细解析Java中处理... 目录摘要概述核心问题:关键技术点:源码解析示例 1:使用HttpURLConnection获取Resp

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

springboot报错Invalid bound statement (not found)的解决

《springboot报错Invalidboundstatement(notfound)的解决》本文主要介绍了springboot报错Invalidboundstatement(not... 目录一. 问题描述二.解决问题三. 添加配置项 四.其他的解决方案4.1 Mapper 接口与 XML 文件不匹配

解决Spring运行时报错:Consider defining a bean of type ‘xxx.xxx.xxx.Xxx‘ in your configuration

《解决Spring运行时报错:Considerdefiningabeanoftype‘xxx.xxx.xxx.Xxx‘inyourconfiguration》该文章主要讲述了在使用S... 目录问题分析解决方案总结问题Description:Parameter 0 of constructor in x

Oracle type (自定义类型的使用)

oracle - type   type定义: oracle中自定义数据类型 oracle中有基本的数据类型,如number,varchar2,date,numeric,float....但有时候我们需要特殊的格式, 如将name定义为(firstname,lastname)的形式,我们想把这个作为一个表的一列看待,这时候就要我们自己定义一个数据类型 格式 :create or repla

Anaconda 中遇到CondaHTTPError: HTTP 404 NOT FOUND for url的问题及解决办法

最近在跑一个开源项目遇到了以下问题,查了很多资料都大(抄)同(来)小(抄)异(去)的,解决不了根本问题,费了很大的劲终于得以解决,记录如下: 1、问题及过程: (myenv) D:\Workspace\python\XXXXX>conda install python=3.6.13 Solving environment: done.....Proceed ([y]/n)? yDownloa

jenkins 插件执行shell命令时,提示“Command not found”处理方法

首先提示找不到“Command not found,可能我们第一反应是查看目标机器是否已支持该命令,不过如果相信能找到这里来的朋友估计遇到的跟我一样,其实目标机器是没有问题的通过一些远程工具执行shell命令是可以执行。奇怪的就是通过jenkinsSSH插件无法执行,经一番折腾各种搜索发现是jenkins没有加载/etc/profile导致。 【解决办法】: 需要在jenkins调用shell脚

Caused by: org.hibernate.MappingException: Could not determine type for: org.cgh.ssh.pojo.GoodsType,

MappingException:这个主要是类映射上的异常,Could not determine type for: org.cgh.ssh.pojo.GoodsType,这句话表示GoodsType这个类没有被映射到

QT 编译报错:C3861: ‘tr‘ identifier not found

问题: QT 编译报错:C3861: ‘tr’ identifier not found 原因 使用tr的地方所在的类没有继承自 QObject 类 或者在不在某一类中, 解决方案 就直接用类名引用 :QObject::tr( )

java.sql.SQLException: No data found

Java代码如下: package com.accord.utils;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import