原文: https://howtodoinjava.com/resteasy/enable-gzip-compression-content-encoding-in-resteasy/

JAX-RS Resteasy 具有自动 GZIP 压缩支持。 如果客户端框架或 JAX-RS 服务接收到具有“gzip”内容编码的消息正文,它将自动对其进行解压缩。 客户端框架自动将Accept-Encoding标头设置为“gzip, deflate”。 因此,您不必自己设置此标头。

要使用 gzip 压缩,请按以下方式使用@GZIP注解。

  1. //Output compression
  2. @GET
  3. @Path("/users")
  4. @GZIP
  5. @Produces("application/xml")
  6. public Users getAllUsers()
  7. {
  8. //more code....
  9. }
  10. //OR
  11. //Input compression
  12. @POST
  13. @Path("/users")
  14. @Consumes("application/vnd.com.demo.user-management.user+xml;charset=UTF-8;version=1")
  15. public Response createUser(@GZIP User user,
  16. @DefaultValue("false") @QueryParam("allow-admin") boolean allowAdmin)
  17. throws URISyntaxException
  18. {
  19. //More code...
  20. }

用法示例

在上面的 GET REST API 上调用时,没有 gzip 压缩的示例 API 输出将如下所示:

  1. Date: Sat, 03 Aug 2013 06:18:41 GMT
  2. Server: Apache-Coyote/1.1
  3. Content-Length: 277
  4. Content-Type: application/vnd.com.demo.user-management.users+xml;version="1";charset=UTF-8

RESTEasy – 启用 Gzip 压缩内容编码 - 图1

不带 gzip 压缩得 RESTEasy 示例

使用@GZIP注解进行 gzip 压缩的示例 API 输出

  1. Date: Sat, 03 Aug 2013 06:31:21 GMT
  2. Content-Encoding: gzip
  3. Server: Apache-Coyote/1.1
  4. Content-Length: 165
  5. Content-Type: application/vnd.com.demo.user-management.users+xml;version="1";charset=UTF-8

RESTEasy – 启用 Gzip 压缩内容编码 - 图2

带有 gzip 压缩的 RESTEasy 示例

给我留言,帖子中还不清楚。

祝您学习愉快!