Andreas Blomqvist
Andreas Blomqvist

Reputation: 437

Why does my response not get zipped when using GZIPContentEncodingFilter

I have a REST method where I want to output gziped content. I have added

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

To the servlet in web.xml

I can see that the code goes thru the GZIPContentEncodingFilter class by debugging but output does not get the .gzip prefix and content is not compressed, instead it is normal json. I am using Jersey 1.14.

Method looks like:

@GET
    @Path("/fundlight")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getFundLightList() {

        StopWatch watch = new StopWatch();
        watch.start();

        Collection<Object> objectResults = null;

        objectResults = getCacheMap("FundLight").values();
        List<FundLight> fundLightList = new ArrayList(objectResults);

        watch.stop();

        GenericEntity<List<FundLight>> entity = new GenericEntity<List<FundLight>>(fundLightList) {
        };


         ResponseBuilder builder = Response.ok(entity);
         return builder.build();

    }

Upvotes: 0

Views: 1870

Answers (2)

koem
koem

Reputation: 574

The easiest approach is to register GZIPEncoder and EncodingFilter:

public class MyApplication extends Application {
    @Override
    public Set<Class<?>> getClasses() {
        final Set<Class<?>> classes = new HashSet<>();

        classes.add(GZipEncoder.class); // this allows gzipped requests and responses
        classes.add(EncodingFilter.class); // this enables any registered encoders

        return classes;
    }
}

Now your server side can process requests with "Content-Encoding:gzip" and respond gzipped when the client request's header had a "Accept-Encoding:gzip".

You could also tell your tomcat / apache / whatever to do response zipping by configuration. Eg. compression="on" in your server.xml's Connectors (Tomcat 7).

Upvotes: 0

cschaefer
cschaefer

Reputation: 1694

I think it depends on the client request header parameters.
If the request contains an Accept-Encoding header containing "gzip" then the response entity (if any) is compressed using gzip and a Content-Encoding header of "gzip" is added to the response.

See: http://jersey.java.net/nonav/apidocs/latest/jersey/com/sun/jersey/api/container/filter/GZIPContentEncodingFilter.html

Upvotes: 3

Related Questions