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