Jersey provides a simple yet elegant way to compress the REST responses. It ships with a EncodingFilter that handles the compression of responses as they are sent to the client. When a request with the Accept-Encoding header is received, the filter first examines what content encodings are supported by the container by listing up all the available ContentEncoder encoders instances. It then decides what encoding should be chosen based on the encodings listed in the Accept-Encoding request header and their associated quality values. If none of the acceptable encodings is supported and identity encoding is explicitly forbidden by the client, the filter generates 406 NOT_ACCEPTABLE response.

This filter is not enabled by default. Therefore we need to enable it in our ResourceConfig.

    import org.glassfish.jersey.server.filter.EncodingFilter;

    public class JerseyEncodingApp extends ResourceConfig {
        public JerseyEncodingApp() {
            register(EncodingFilter.class);
        }
    }

That's it! Now all we need are encoders. Out of the box, Jersey supports two of the most widely used content encodings - gzip and deflate. Once we have enabled the EncodingFilter , we can register the encoders.

    import org.glassfish.jersey.server.filter.EncodingFilter;
    import org.glassfish.jersey.message.DeflateEncoder;
    import org.glassfish.jersey.message.GZipEncoder;

    public class JerseyEncodingApp extends ResourceConfig {
        public JerseyEncodingApp() {
            register(EncodingFilter.class);
            register(GZipEncoder.class);
            register(DeflateEncoder.class);
        }
    }