【问题标题】:gZip Implementations in REST APIsREST API 中的 gZip 实现
【发布时间】:2017-03-07 06:39:48
【问题描述】:

如何在 REST API 中使用 gzip 过滤器? 另外,假设我想在一个地方实现。有没有办法在 20 个 API 中进行配置,只有少数 API 使用它。

任何文档都会有所帮助。

【问题讨论】:

  • 您能澄清一下您的问题吗?我真的不明白你的意思。
  • @CássioMazzochiMolin:已编辑。请让我知道它是否可以澄清您的疑问
  • 你能展示一下你到目前为止所做的尝试吗?
  • 只要有 REST API 实现。正在网上搜索以了解如何做到这一点。
  • 你的 JAX-RS 实现是什么?

标签: java performance jax-rs gzip


【解决方案1】:

这可以通过WriterInterceptor来实现:

public class GZIPWriterInterceptor implements WriterInterceptor {

    @Override
    public void aroundWriteTo(WriterInterceptorContext context)
                throws IOException, WebApplicationException {
        final OutputStream outputStream = context.getOutputStream();
        context.setOutputStream(new GZIPOutputStream(outputStream));
        context.proceed();
    }
}

然后在您的ResourceConfig / Application 子类中注册WriterInterceptor

@ApplicationPath("/api")
public class MyApplication extends ResourceConfig {

    public MyApplication() {
        register(GZIPWriterInterceptor.class);
    }
}

要将拦截器绑定到某些资源方法或类,您可以使用name binding annotations

更多详情,请查看Jersey documentation about filters and interceptors

【讨论】:

  • 它有效。尽管您可能需要在响应中添加标头
猜你喜欢
  • 2018-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-17
  • 1970-01-01
  • 2017-06-05
  • 2015-10-26
  • 2013-12-25
相关资源
最近更新 更多