JAX-RS Resteasy 具有自动 GZIP 解压支持。如果客户端框架 或 JAX-RS 服务收到内容编码为“gzip”的消息正文,它会自动解压缩它。客户端框架自动将 Accept-Encoding 标头设置为“gzip, deflate”。所以你不必自己设置这个标题。
要使用 gzip 压缩,请使用 @GZIP 注释如下。
//Output compression @GET @Path("/users") @GZIP @Produces("application/xml") public Users getAllUsers() { //more code.... } //OR //Input compression @POST @Path("/users") @Consumes("application/vnd.com.demo.user-management.user+xml;charset=UTF-8;version=1") public Response createUser(@GZIP User user, @DefaultValue("false") @QueryParam("allow-admin") boolean allowAdmin) throws URISyntaxException { //More code... }
示例 API 输出没有 gzip 压缩,当调用上面的 GET REST API 时,将如下所示:
Date: Sat, 03 Aug 2013 06:18:41 GMT Server: Apache-Coyote/1.1 Content-Length: 277 Content-Type: application/vnd.com.demo.user-management.users+xml;version="1";charset=UTF-8
API 输出示例 使用@GZIP 注释进行 gzip 压缩
Date: Sat, 03 Aug 2013 06:31:21 GMT Content-Encoding: gzip Server: Apache-Coyote/1.1 Content-Length: 165 Content-Type: application/vnd.com.demo.user-management.users+xml;version="1";charset=UTF-8
给我评论,帖子中有一些不清楚的地方。
快乐学习!!
地址:https://www.cundage.com/article/enable-gzip-compression-content-encoding-in-resteasy.html