码迷,mamicode.com
首页 > 其他好文 > 详细

关于feign调用的异常

时间:2021-05-24 05:30:08      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:导致   col   lse   jar   stringbu   efault   方式   line   ide   

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;
}

关于feign调用的异常

标签:导致   col   lse   jar   stringbu   efault   方式   line   ide   

原文地址:https://www.cnblogs.com/woshuaile/p/14754978.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!