阅读 45 SEO

关于feign调用的异常

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

来源:https://www.cnblogs.com/woshuaile/p/14754978.html

 服务器测评http://www.cncsto.com/


文章分类
后端
文章标签
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 gxwowoo@163.com 举报,一经查实,本站将立刻删除。
相关推荐