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);
    }
}
相关推荐
编程、小哥哥1 小时前
Java面试场景:从音视频到AI应用的技术探讨
spring boot·spring cloud·微服务·音视频·java面试·ai应用
lichuangcsdn6 小时前
【springcloud学习(dalston.sr1)】Eureka服务端集群的搭建(含源代码)(二)
学习·spring cloud·eureka
Hello World......9 小时前
互联网大厂Java面试:从Spring到微服务的全面探讨
java·spring boot·spring cloud·微服务·面试·技术栈·互联网大厂
lichuangcsdn1 天前
【springcloud学习(dalston.sr1)】Eureka 客户端服务注册(含源代码)(四)
学习·spring cloud·eureka
残花月伴2 天前
springCloud/Alibaba常用中间件之Seata分布式事务
分布式·spring cloud·中间件
zy happy2 天前
搭建运行若依微服务版本ruoyi-cloud最新教程
java·spring boot·spring cloud·微服务·ruoyi
IT成长史2 天前
deepseek梳理java高级开发工程师微服务面试题-进阶版
java·spring cloud·微服务
紫璨月2 天前
Feign异步模式丢失上下文问题
spring cloud·openfeign
Volunteer Technology2 天前
SpringCloud Gateway知识点整理和全局过滤器实现
spring·spring cloud·gateway
残花月伴3 天前
springCloud/Alibaba常用中间件之Setinel实现熔断降级
spring·spring cloud·中间件