2.12.4. GZIP 压缩与解压缩

RESTEasy 支持 GZIP 压缩和解压缩。为了支持 GZIP 解压缩,客户端框架或 JAX-RS 服务会自动解压缩具有 gzip Content-Encoding 的消息正文,并且它可以自动将 Accept-Encoding 标头设置为 gzip,以便您不必手动设置此标头。若要支持 GZIP 压缩,如果客户端框架正在发送请求,或者服务器正在发送一个响应( Content-Encoding 标头设为 gzip ),RESTEasy 会压缩传出消息。您可以使用 @org.jboss.resteasy.annotation.GZIP 注释来设置 Content-Encoding 标头。

以下示例标记传出消息正文 进行 gzip 压缩的顺序

示例:GZIP 压缩

@Path("/")
public interface MyProxy {

   @Consumes("application/xml")
   @PUT
   public void put(@GZIP Order order);
}

示例:GZIP 压缩服务器响应标签

@Path("/")
public class MyService {

   @GET
   @Produces("application/xml")
   @GZIP
   public String getData() {...}
}

2.12.4.1. 配置 GZIP 压缩和解压缩

注意

RESTEasy 默认禁用 GZIP 压缩和解压缩,以防止对可能较大但已被攻击者压缩并发送到服务器的实体解压缩。

有三个与 GZIP 压缩和解压缩相关的拦截器:

  • org.jboss.resteasy.plugins.interceptors.GZIPDecodingInterceptor :如果 Content-Encoding 标头存在并且值为 gzipGZIPDecodingInterceptor 将安装 解压缩邮件正文的输入 流。
  • org.jboss.resteasy.plugins.interceptors.GZIPEncodingInterceptor :如果 Content-Encoding 标头存在并且值为 gzipGZIPEncodingInterceptor 会安装 压缩邮件正文的输出 流。
  • org.jboss.resteasy.plugins.interceptors.AcceptEncodingGZIPFilter :如果 Accept-Encoding 标头不存在,Accept EncodingGZIPFilter 会添加值为 gzip 的 Accept-Encoding 标头。如果 Accept-Encoding 标头存在但不包含 gzip,AcceptEncodingGZIPFilter 拦截器将附加该值 gzip

    注意

    启用 GZIP 压缩或解压缩并不依赖于 AcceptEncodingGZIPFilter 拦截器的存在性。

启用 GZIP 解压缩器可以为从压缩消息正文中提取的 GZIPDecodingInterceptor 的字节数设置上限。默认限值为 10,000,000