zoukankan      html  css  js  c++  java
  • 关于feign调用的异常

    Feign声明式调用服务 feign.codec.DecodeException: Error while extracting response for type [class **] and...

    日前在做项目时候遇到feign调用出现返回乱码,导致程序出错事件。原因是因为cloud的依赖版本过低,feign不支持 gzip 解码。在不升级jar的情况下,增加了一个过滤器。 

    代码如下:

    public class FeignResponseDecoder implements Decoder {
        private final Decoder delegate;
    
        public FeignResponseDecoder(Decoder delegate) {
            Objects.requireNonNull(delegate, "Decoder must not be null. ");
            this.delegate = delegate;
        }
    
        @Override
        public Object decode(Response response, Type type) throws IOException {
            Collection<String> values = response.headers().get(HttpEncoding.CONTENT_ENCODING_HEADER);
            if (Objects.nonNull(values) && !values.isEmpty() && values.contains(HttpEncoding.GZIP_ENCODING)) {
                byte[] compressed = Util.toByteArray(response.body().asInputStream());
                if ((compressed == null) || (compressed.length == 0)) {
                    return delegate.decode(response, type);
                }
                //decompression part
                //after decompress we are delegating the decompressed response to default
                //decoder
                if (isCompressed(compressed)) {
                    final StringBuilder output = new StringBuilder();
                    final GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(compressed));
                    final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(gis, StandardCharsets.UTF_8));
                    String line;
                    while ((line = bufferedReader.readLine()) != null) {
                        output.append(line);
                    }
                    Response uncompressedResponse = response.toBuilder().body(output.toString().getBytes()).build();
                    return delegate.decode(uncompressedResponse, type);
                } else {
                    return delegate.decode(response, type);
                }
            } else {
                return delegate.decode(response, type);
            }
        }
    
        private static boolean isCompressed(final byte[] compressed) {
            return (compressed[0] == (byte) (GZIPInputStream.GZIP_MAGIC)) && (compressed[1] == (byte) (GZIPInputStream.GZIP_MAGIC >> 8));
        }
    }
    

      

    再把这个类,以bean的方式注入即可:

    @Bean
    public Decoder GZIPResponseDecoder(ObjectFactory<HttpMessageConverters> messageConverters) {
    Decoder decoder = new FeignResponseDecoder(new SpringDecoder(messageConverters));
    return decoder;
    }
  • 相关阅读:
    ELF静态链接
    linux
    百度图表插件
    秀米(图文编辑)
    Html 助手
    在型原型工具
    AnalyticDB
    H5 (webApi) 接口帮助文档
    H5 拖拽读取文件和文件夹
    mongodb int字段的一个小坑
  • 原文地址:https://www.cnblogs.com/woshuaile/p/14754978.html
Copyright © 2011-2022 走看看