RESTEasy – Enable Gzip Compression Content Encoding

JAX-RS Resteasy has automatic GZIP decompression support. If the client framework or a JAX-RS service receives a message body with a Content-Encoding of “gzip”, it will automatically decompress it. The client framework automatically sets the Accept-Encoding header to be “gzip, deflate”. So you do not have to set …

JAX-RS Resteasy has automatic GZIP decompression support. If the client framework or a JAX-RS service receives a message body with a Content-Encoding of “gzip”, it will automatically decompress it. The client framework automatically sets the Accept-Encoding header to be “gzip, deflate”. So you do not have to set this header yourself.

To use gzip compression, use @GZIP annotation in following manner.

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

Example Usages

Example API output without gzip compression, when calling above GET REST API, will be as follows:

	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
RESTEasy example without gzip compression

Example API output with gzip compression using @GZIP annotation

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
RESTEasy example with gzip compression

Drop me a comment, something is not clear from the post.

Happy Learning !!

Leave a Comment

  1. Can you show me the actual response body?
    Also, please give an example on how to send gzip data in the request.

    Reply
  2. Hi Lokesh,

    I did try the example above for my project (Restful Web services), but i was unable to find the compression working. Most of the tools that i have tested still showed the me ” Transfer-Encoding: chunked” . Was unable to find the content-encoding as “gzip” any where. Any help is highly appreciated.

    Reply
    • Please let me know:
      1) what all request headers you are sending in request?
      2) Method type: i.e. GET or POST or else?
      3) Are you getting response back?

      If possible please provide which environment you are using?

      Reply
      • I have created Java based web services using jersey framework. The method that i was trying was a simple get method transmitting in Json format. I do receive the response as expected but not as GZIP’ed data, when viewed in tools like poster, etc… i am able to see the following details :

        Response:
        status: 200 OK
        Date: Tue, 15 Apr 2014 03:26:10 GMT
        Server: Apache-Coyote/1.1
        Transfer-Encoding: chunked
        Content-Type: application/json

        I am using eclipse as my dev env.

        Thanks, Lokesh

        Reply
        • For Jersey, you should be using:

          <init-param>
          <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
          <param-value>com.sun.jersey.api.container.filter.GZIPContentEncodingFilter</param-value>
          </init-param>
          <init-param>
          <param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
          <param-value>com.sun.jersey.api.container.filter.GZIPContentEncodingFilter</param-value>
          </init-param>

          Reply
          • Eureka!!! Great … thanks lokesh… I tried what u suggested and it works. But just one question – Due to the fix that i have included in my web.xml, any request and response will be in gzip format irrespective of the use of @GZIP annotation. Am I right?

Leave a Comment

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.