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);
    }
}
相关推荐
2501_9339232510 小时前
SpringCloud组件:Eureka服务注册与发现
java·开发语言·后端·spring cloud·eureka
叶总没有会19 小时前
Gateway--网关
spring cloud·微服务·gateway
海马1 天前
Spring Cloud 开发知识整理
spring·spring cloud·wpf
栗子~~1 天前
SpringCloud Gateway 基于 Nacos 实现动态路由
java·spring cloud·gateway
code_slave(码畜)1 天前
微服务架构落地:消息队列架构设计(上篇)——异步解耦、削峰填谷,看懂业务事件流转本质
spring boot·spring cloud·微服务·云原生·架构
code_slave(码畜)2 天前
微服务架构落地:消息队列架构设计(下篇)——消息堆积、死信治理与集群监控告警实战
spring boot·spring cloud·微服务·云原生·中间件·架构
知守观2 天前
feign-core 就在依赖树里,运行时却找不到类:一次 provided scope 引发的启动失败排查
java·spring cloud·maven
阿狗童鞋2 天前
高并发系统设计实战指南
java·spring boot·spring cloud
for_ever_love__2 天前
MySQL 高频面试题 30 问:索引、事务、锁、日志、主从与分库分表(MySQL 8.0 版)
java·数据库·spring boot·mysql·spring cloud·面试·总结
天空鸟_时光不老3 天前
13-亮点提炼:把技术说辞翻译成业务价值
java·spring boot·spring·spring cloud·mybatis