RESTEasy – 启用 Gzip 压缩内容编码

RESTEasy – 启用 Gzip 压缩内容编码

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

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...
	}

用法示例

在上面的 GET REST API 上调用时,没有 gzip 压缩的示例 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

RESTEasy example without gzip compression

不带 gzip 压缩得 RESTEasy 示例

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

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

RESTEasy example with gzip compression

带有 gzip 压缩的 RESTEasy 示例

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

祝您学习愉快!