• 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>]  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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    pom.xml 依赖

    <dependency>
    	<groupId>org.springframework.cloudgroupId>
    	<artifactId>spring-cloud-starter-openfeignartifactId>
    	<version>3.0.7version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    2. 分析
    项目启动,调用接口,有时报错,重启后调用相同接口,有时好,有时报错,分析发现是feign接口返回值反序列化时无法处理,自定义处理后正常了
    
    • 1
    3. 自定义返回值解析器
    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>]  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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    配置FeignResponseInterceptor

    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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
  • 相关阅读:
    参加2024年CSDN博客_付费内容优质创作者_颁奖仪式的收获
    介绍五个很实用的IDEA使用技巧
    ZJU Beamer学习手册(二)
    无病呻吟之高三回忆随笔@_@
    Go单体服务开发最佳实践
    怎么获取别人店铺的商品呢?
    CLR C#--引用类型和值类型
    C++实战-Linux多线程(入门到精通)
    Python和Numpy的加权抛硬币游戏的概率
    Roson的Qt之旅 #112 QML布局之GridLayout(表格布局)
  • 原文地址:https://blog.csdn.net/m0_37539286/article/details/133026711