A few days ago, when I was working on a project, I encountered a garbled return from the feign call, which caused a program error. The reason is that the dependency version of cloud is too low, and feign does not support gzip decoding. Without upgrading the jar, a filter was added.
code show as below:
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));
}
}
Then inject this class as a bean:
@Bean
public Decoder GZIPResponseDecoder(ObjectFactory<HttpMessageConverters> messageConverters) {
Decoder decoder = new FeignResponseDecoder(new SpringDecoder(messageConverters));
return decoder;
}
Read More:
- Error (17) solves the problem of losing the request header of multithread asynchronous feign call
- [Solved] Error while extracting response for type [] and content type []…
- feign.FeignException: status 404 reading XXXClient#XXMethod(String)
- [Solved] Feign Error: -‘oauth-client.FeignClientSpecification‘ could not be registered
- Mac Docker pull Error: Error response from daemon: Get https://xx.xx.xx.xx/v2/: Service Unavailable
- AAR Call Error: only jar-type local dependencies are supported
- [Solved] The type or namespace name ‘Service’ does not exist Error
- Win7 Install .NET Extracting Files: There is not enough disk space……
- maven Error: Element ‘dependency’ cannot have character [children], because the type’s content type is
- [Solved] The method getContextPath() from the type HttpServletRequest refers to the missing type String
- Libtorch Error: Expected object of type Variable but found type CUDALongType for argument #2 ‘index’
- SharePoint site shows ‘Service Unavailable. HTTP Error 503. The service is unavailable’
- [Solved] TypeError: super(type, obj): obj must be an instance or subtype of type
- [Go] Solve the empty interface interface{} cannot use (type []string) as type []interface {}
- jupyter notebook Sets Default Browser Error: SyntaxError: (unicode error) ‘utf-8‘ codec can‘t decode byte 0xd4
- How to Solve Error: Element ‘dependency’ cannot have character [children], because the type’s content type is element-
- ubuntu20.04——hdaudioC0D2: unable to bind the codec
- Docker service startup error: Job for docker.service failed because the control process exited with error code.
- [Solved] Synchronous operations are disallowed. Call ReadAsync or set AllowSynchronousIO to true instead.
- [Solved] hytrix service degraded bean cannot inject error