Feign实现GZIP压缩 尤其时数据发过来之后的解压缩

https://blog.csdn.net/qq_43097257/article/details/105010340

反正就是有个对gzip过滤器 fegin默认的Client对响应流不支持对gzip后的字节流进行解析,所以在序列化成对象时会存在解析问题。

会报这个错 

 Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Illegal character ((CTRL-CHAR, code 31)): only regular white space (\r, \n, \t) is allowed between tokens; nested exception is com.fasterxml.jackson.core.JsonParseException: Illegal character ((CTRL-CHAR, code 31)): only regular white space (\r, \n, \t

 

 

那为什么以前的项目有 不配这个 就管用呢 是因为以前的项目用的eureka做的注册中心 这里面有一个关键的

就是这个 但是用nacos做注册中心没有这个类 所以只能自己写了  记住 这

HttpServletRequestWrapper 是很关键的东西 请求过来就来这里
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Feign默认并没有提供数据压缩的功能,但我们可以通过自定义编解码器的方式来实现数据压缩。 具体实现步骤如下: 1. 创建自定义编解码器 可以使用GZIP进行数据压缩,然后使用Base64进行编码。创建一个实现了`feign.codec.Encoder`和`feign.codec.Decoder`接口的编解码器实现类,具体实现如下: ```java public class GzipEncoderDecoder implements Encoder, Decoder { private final Encoder delegateEncoder; private final Decoder delegateDecoder; public GzipEncoderDecoder(Encoder delegateEncoder, Decoder delegateDecoder) { this.delegateEncoder = delegateEncoder; this.delegateDecoder = delegateDecoder; } @Override public Object decode(Response response, Type type) throws IOException, DecodeException, FeignException { String encoding = response.headers().getOrDefault("Content-Encoding", ""); if (encoding.contains("gzip")) { GZIPInputStream gzipInputStream = new GZIPInputStream(response.body().asInputStream()); return delegateDecoder.decode(response.toBuilder().body(gzipInputStream).build(), type); } return delegateDecoder.decode(response, type); } @Override public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(outputStream)) { delegateEncoder.encode(object, bodyType, new RequestTemplateAdapter(template, gzipOutputStream)); } catch (IOException e) { throw new EncodeException(e.getMessage(), e); } template.header("Content-Encoding", "gzip"); template.body(outputStream.toByteArray(), null); } private static class RequestTemplateAdapter extends RequestTemplate { private final RequestTemplate delegate; private final OutputStream outputStream; private RequestTemplateAdapter(RequestTemplate delegate, OutputStream outputStream) { this.delegate = delegate; this.outputStream = outputStream; } @Override public void body(byte[] data, Charset charset) { try { outputStream.write(data); outputStream.flush(); } catch (IOException e) { throw new RuntimeException(e); } } @Override public Collection<String> headerNames() { return delegate.headerNames(); } @Override public Collection<String> headers(String key) { return delegate.headers(key); } @Override public void header(String key, String value) { delegate.header(key, value); } @Override public String method() { return delegate.method(); } @Override public void method(String method) { delegate.method(method); } @Override public void uri(String uri) { delegate.uri(uri); } } } ``` 2. 注册自定义编解码器 在Feign客户端配置类中注册自定义编解码器,如下所示: ```java @Configuration public class FeignClientConfiguration { @Bean public Decoder decoder() { return new GzipEncoderDecoder(new JacksonDecoder(), new JacksonDecoder()); } @Bean public Encoder encoder() { return new GzipEncoderDecoder(new JacksonEncoder(), new JacksonDecoder()); } } ``` 这样就可以在Feign客户端中使用数据压缩功能了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值