openfeign返回消息报错.UnknownContentTypeException

1. springcloud项目使用openfeign报错
复制代码
org.springframework.web.client.UnknownContentTypeException: Could not extract response: no suitable HttpMessageConverter found for response type [com.yl.base.Result<java.util.List<com.yl.entity.LabelConfig>>]  and content type [application/json]
	at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:126)
	at org.springframework.cloud.openfeign.support.SpringDecoder.decode(SpringDecoder.java:57)
	at org.springframework.cloud.openfeign.support.ResponseEntityDecoder.decode(ResponseEntityDecoder.java:61)
	at feign.AsyncResponseHandler.decode(AsyncResponseHandler.java:115)
	at feign.AsyncResponseHandler.handleResponse(AsyncResponseHandler.java:87)
	at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:138)

pom.xml 依赖

xml 复制代码
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-openfeign</artifactId>
	<version>3.0.7</version>
</dependency>
2. 分析
复制代码
项目启动,调用接口,有时报错,重启后调用相同接口,有时好,有时报错,分析发现是feign接口返回值反序列化时无法处理,自定义处理后正常了
3. 自定义返回值解析器
java 复制代码
import com.alibaba.fastjson.JSON;
import feign.FeignException;
import feign.Response;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.SpringDecoder;

import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
/**
 * Feign接口响应数据处理
 */
public class FeignResponseInterceptor extends SpringDecoder {
 public FeignResponseInterceptor(ObjectFactory<HttpMessageConverters> messageConverters) {
        super(messageConverters);
    }

    @Override
    public Object decode(final Response response, Type type) throws IOException, FeignException {
        Response.Body body = response.body();
        String bodyString = IOUtils.toString(body.asReader(StandardCharsets.UTF_8));
        //响应日志

        // 此处有bug,有时没问题,有时报错
        // org.springframework.web.client.UnknownContentTypeException: Could not extract response: no suitable HttpMessageConverter found for response type [com.yl.base.Result<java.util.List<com.yl.entity.LabelConfig>>]  and content type [application/json]
//        Object o = super.decode(response.toBuilder().body(bodyString, StandardCharsets.UTF_8).build(), type);
        //改为fastjson反序列化
        Object o = JSON.parseObject(bodyString).toJavaObject(type);
        return o;
    }
}

配置FeignResponseInterceptor

java 复制代码
import feign.codec.Decoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

import java.util.ArrayList;
import java.util.List;
@Configuration
public class FeignConfig {

    @Primary
    @Bean
    public Decoder decoder() {
        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();

        List<MediaType> supportedMediaTypes = new ArrayList<>();
        supportedMediaTypes.add(MediaType.APPLICATION_JSON);
        jackson2HttpMessageConverter.setSupportedMediaTypes(supportedMediaTypes);

        ObjectFactory objectFactory = () -> new HttpMessageConverters(jackson2HttpMessageConverter);
        return new FeignResponseInterceptor(objectFactory);
    }
}
相关推荐
Jul1en_11 小时前
【Java 脚手架】封装通用工具类-1
java·spring boot·分布式·spring·spring cloud
史呆芬3 天前
分布式事务实战:微服务跨服务数据一致性解决方案
java·后端·spring cloud
niaiheni3 天前
红队实战:记一次Spring Cloud Gateway SpEL表达式注入到哥斯拉内存马(CVE-2022-22947)
web安全·spring cloud·网络安全
zzzll11114 天前
Spring Cloud 微服务:从入门到实践
spring·spring cloud·微服务
Devin~Y4 天前
从本地生活电商到 AI RAG:互联网大厂 Java 面试场景完整实战
java·spring boot·redis·elasticsearch·spring cloud·kafka·rag
小罗水4 天前
第11章 PostgreSQL + pgvector 向量检索
java·数据库·spring cloud·微服务
爱学习的小可爱卢5 天前
SpringCloud——深入解析SkyWalking源码:TraceProfiling追踪慢方法
spring cloud·微服务·skywalking
小罗水5 天前
第8章 文档解析与文本切片
数据库·spring·spring cloud·微服务
一知半解仙5 天前
从0到1!Spring Cloud微服务无缝集成AI智能体:架构设计+核心源码+生产落地全指南
人工智能·spring cloud·微服务
小罗水8 天前
第3章 从单体项目到 Spring Cloud 微服务
spring·spring cloud·微服务