相关文章推荐
英俊的山楂  ·  使用 Azure Developer ...·  2 月前    · 
害羞的大象  ·  docker环境 ...·  1 年前    · 
谈吐大方的筷子  ·  如何实现在jupyter ...·  1 年前    · 
RestTemplate请求:Could not extract response: no suitable HttpMessageConverter found for response type 2021-09-23 11:03:24

1、问题描述

使用RestTemplate发请求时出现
Could not extract response: no suitable HttpMessageConverter found for response type.... content type [text/html;charset=UTF-8]
的问题,具体错误如下:

org.springframework.web.client.UnknownContentTypeException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.tyj.study.rest_template.ResponseB] and content type [text/html;charset=UTF-8]
    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:126) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:998) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:981) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:741) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:674) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:342) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]

2、问题原因

RestTemplate请求不支持content type [text/html;charset=UTF-8]类型

MappingJackson2HttpMessageConverter

package org.springframework.http.converter.json;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.MediaType;
import org.springframework.lang.Nullable;
 * Implementation of {@link org.springframework.http.converter.HttpMessageConverter} that can read and
 * write JSON using <a href="https://github.com/FasterXML/jackson">Jackson 2.x's</a> {@link ObjectMapper}.
 * <p>This converter can be used to bind to typed beans, or untyped {@code HashMap} instances.
 * <p>By default, this converter supports {@code application/json} and {@code application/*+json}
 * with {@code UTF-8} character set. This can be overridden by setting the
 * {@link #setSupportedMediaTypes supportedMediaTypes} property.
 * <p>The default constructor uses the default configuration provided by {@link Jackson2ObjectMapperBuilder}.
 * <p>Compatible with Jackson 2.9 to 2.12, as of Spring 5.3.
 * @author Arjen Poutsma
 * @author Keith Donald
 * @author Rossen Stoyanchev
 * @author Juergen Hoeller
 * @author Sebastien Deleuze
 * @since 3.1.2
public class MappingJackson2HttpMessageConverter extends AbstractJackson2HttpMessageConverter {
	@Nullable
	private String jsonPrefix;
	 * Construct a new {@link MappingJackson2HttpMessageConverter} using default configuration
	 * provided by {@link Jackson2ObjectMapperBuilder}.
	public MappingJackson2HttpMessageConverter() {
		this(Jackson2ObjectMapperBuilder.json().build());
	 * Construct a new {@link MappingJackson2HttpMessageConverter} with a custom {@link ObjectMapper}.
	 * You can use {@link Jackson2ObjectMapperBuilder} to build it easily.
	 * @see Jackson2ObjectMapperBuilder#json()
	public MappingJackson2HttpMessageConverter(ObjectMapper objectMapper) {
		super(objectMapper, MediaType.APPLICATION_JSON, new MediaType("application", "*+json"));
	 * Specify a custom prefix to use for this view's JSON output.
	 * Default is none.
	 * @see #setPrefixJson
	public void setJsonPrefix(String jsonPrefix) {
		this.jsonPrefix = jsonPrefix;
	 * Indicate whether the JSON output by this view should be prefixed with ")]}', ". Default is false.
	 * <p>Prefixing the JSON string in this manner is used to help prevent JSON Hijacking.
	 * The prefix renders the string syntactically invalid as a script so that it cannot be hijacked.
	 * This prefix should be stripped before parsing the string as JSON.
	 * @see #setJsonPrefix
	public void setPrefixJson(boolean prefixJson) {
		this.jsonPrefix = (prefixJson ? ")]}', " : null);
	@Override
	protected void writePrefix(JsonGenerator generator, Object object) throws IOException {
		if (this.jsonPrefix != null) {
			generator.writeRaw(this.jsonPrefix);

通过51-53行的代码

	public MappingJackson2HttpMessageConverter(ObjectMapper objectMapper) {
		super(objectMapper, MediaType.APPLICATION_JSON, new MediaType("application", "*+json"));

可以看出RestTemplate 实例化过程时 只支持application/json格式

3、解决办法

在实例化RestTemplate时 手动添加text/plan,text/html格式

 @Bean("restTemplate")
    public RestTemplate restTemplate(){
        RestTemplate restTemplate = new RestTemplate();
        MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        mappingJackson2HttpMessageConverter.setSupportedMediaTypes(Arrays.asList(
                MediaType.TEXT_HTML,
                MediaType.TEXT_PLAIN));
        restTemplate.getMessageConverters().add(mappingJackson2HttpMessageConverter);
        return restTemplate;
                                    异常信息如下
_Could not extract response: no suitable HttpMessageConverter found for response type [java.util.List<java.util.Ma
p<java.lang.String, java.lang.Object>>] and content type [applicat...
                                    springboot使用RestTemplate报错:Could not extract response: no suitable HttpMessageConverter found for response type [class xx] and …
spring boot版本大于1.4的时候,spring boot不会再自动装配定义一个RestTemplate,需要手动创建,用一下代...
                                    【错误复盘】Could not extract response: no suitable HttpMessageConverter found for response 【xxx】and content type 【text/json;charset=UTF-8】
系统要对接飞鹅云打印机进行打印,需要通过指定接口打印数据
因为使用SpringBoot开发,刚好用RestTemplate+postForObject发送POST请求并接受结果。
设置请求头(Content-Type)
接口要求请求头中要带Content-Type: application/x-www-form-urlencoded
先创建个实现ClientHttpRequestInterceptor接口的类,在intercept(…)方法中对请求头设置,在使用RestTemplate的时
                                    使用 Spring Boot 写项目,需要用到微信接口获取用户信息。
在 Jessey 和 Spring RestTemplate 两个 Rest 客户端中,想到尽量不引入更多的东西,然后就选择了 Spring RestTemplate 作为 网络请求的 Client,然后就被微信接口摆了一道,然后踩了一个 RestTemplate 的坑。
二、第一个坑:被微信摆了一道
报错信息是:
org.springframework.web.client.RestClientException: Could n
                                    异常信息 :
使用feign 调用异常
feign.FeignException$MethodNotAllowed: status 405 reading ConsumerService#findById(Integer)
详细信息:
feign.FeignException$MethodNotAllowed: status 405 reading ConsumerService#findById...
                                    项目中需要调用微信接口获取access_token等一系列和微信接口相关的操作,我使用了Spring自带的RestTemplate类来发送Get或Post请求,直接在Spring配置文件中依赖注入
&amp;lt;bean id=&quot;restTemplate&quot; class=&quot;org.springframework.web.client.RestTemplate&quot;/&amp;gt;
使用的时候直接
@Resourc...
                                    一、问题解释(想看总结的去最下面)
org.springframework.web.client.UnknownContentTypeException: Could not extract response: no suitable HttpMessageConverter found for response type [class XXX] and content type [XXX;XXX]
凡是报这个错误,翻译成人话就是
没有一个自带的转换器能把[class XXX]转换成content ty
                                    远程调用的项目使用PHP写的,因此我只能通过页面的请求参数封装请求Vo,然后调用PHP项目的接口封装响应类。
① 请求体:Feign接口中请求体的字段要比调用的PHP项目中接口的请求体字段少
@Data
public class AssetListQo {
    @JsonProperty(value = "time_range")
    private String timeRange;
    @JsonProperty(value = "branch_id")
    private Strin