Quick revision
The short answer
The client advertises Accept-Encoding: gzip, the server compresses the body and marks it with Content-Encoding: gzip, and it adds Vary: Accept-Encoding so caches keep the compressed and uncompressed copies apart. In Jersey we enable it with EncodingFilter.enableFor(resourceConfig, GZipEncoder.class); in RESTEasy we enable its gzip support (the resteasy.allowGzip setting and the @GZIP annotation, check your version). In many deployments compression is better done once at the reverse proxy or the servlet container, and in every case small bodies and already compressed formats should be skipped.
What to remember
In this answer, we saw that compression is a negotiation: Accept-Encoding from the client, Content-Encoding on the response, and Vary: Accept-Encoding so caches keep separate copies. In Jersey we enable it with EncodingFilter.enableFor and GZipEncoder, and in RESTEasy with its gzip support, resteasy.allowGzip and @GZIP, depending on the version. Often the reverse proxy or container is the better single place to compress, as long as we never compress twice, and we skip small bodies and formats that are already compressed.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The client advertises Accept-Encoding: gzip, the server compresses the body and marks it with Content-Encoding: gzip, and it adds Vary: Accept-Encoding so caches keep the compressed and uncompressed copies apart. In Jersey we enable it with EncodingFilter.enableFor(resourceConfig, GZipEncoder.class); in RESTEasy we enable its gzip support (the resteasy.allowGzip setting and the @GZIP annotation, check your version). In many deployments compression is better done once at the reverse proxy or the servlet container, and in every case small bodies and already compressed formats should be skipped.
Why this question is asked
JSON compresses extremely well, often to a fifth or a tenth of its size, so compression is one of the cheapest ways to make an API faster on slow networks and cheaper on egress. Interviewers ask about it because the three headers involved are frequently misunderstood, because each JAX-RS implementation enables it differently, and because the right answer is often not to do it in the application at all. In this answer, we will recap the negotiation between client and server, enable gzip in Jersey and RESTEasy, see why Vary matters, decide where compression should happen, and list what should not be compressed.
1A Quick Recap: How Client and Server Agree on Compression
Compression in HTTP is a negotiation. The client says what it can decode with Accept-Encoding, for example Accept-Encoding: gzip, deflate, br. The server is free to pick one of those or none. If it compresses, it says so with Content-Encoding: gzip, and the client decompresses before handing the body to the application. The Content-Type stays the same, application/json, because the content is still JSON; only its transfer form changed.
Compression changes the bytes but not the meaning. That is why a third header is needed for caches, which we will come to after the implementation.
7 messages in order. Play them, or step through them one at a time.
2Enabling gzip in Jersey
Jersey implements compression with an EncodingFilter, which looks at Accept-Encoding, and encoders such as GZipEncoder and DeflateEncoder, which do the work as WriterInterceptors around the entity stream.
Notice that the client must ask: a curl without the header gets the plain body. The same registration also lets Jersey decode request bodies sent with Content-Encoding: gzip, which is convenient and, as a separate question about decompression bombs shows, needs limits. On the client side, registering GZipEncoder on a Jersey Client makes it accept and decode compressed responses.
3Enabling gzip in RESTEasy
RESTEasy has its own gzip interceptors. In recent versions they are not active by default; we turn them on with the resteasy.allowGzip configuration property (a context parameter or system property) or by registering the gzip providers, and the @GZIP annotation on a method or class forces compression of that response. The exact switches have changed between RESTEasy versions, so check your version's documentation.
4Why Vary: Accept-Encoding Matters
A shared cache such as a CDN stores responses by URL. Without more information, it would store the gzip version of /products and later serve it to a client that never said it could decode gzip, which would see garbage. Vary: Accept-Encoding tells the cache that the response depends on that request header, so it must keep separate entries, one per encoding, and pick the right one. Any layer that compresses must add it, and it should be repeated on 304 responses. Most CDNs normalise Accept-Encoding to a few values so that the endless variety of browser header strings does not split the cache into thousands of entries.
5Where Should Compression Happen?
Compression can happen in the application (Jersey or RESTEasy), in the servlet container (Tomcat's compression connector attribute, Undertow's encoding handler), or at the reverse proxy or CDN (nginx gzip, Envoy's compressor filter, CDN edge compression). In many deployments the proxy is the best place: one configuration for every service, it can offer Brotli to browsers, and application threads spend no CPU on it.
The thing to avoid is doing it twice. If both the application and the proxy compress, the proxy either recompresses already compressed data, wasting CPU, or has to detect it and skip it. So we choose one layer deliberately and turn it off elsewhere. Application-level compression still makes sense for service-to-service traffic that bypasses the proxy, or for one endpoint with special needs.
| Layer | Strength | Weakness |
|---|---|---|
| Application (Jersey, RESTEasy) | per-endpoint control | CPU on request threads, configured per service |
| Servlet container | no code changes | one more config per service |
| Reverse proxy or CDN | one setting for all, Brotli for browsers | internal calls that skip the proxy are not covered |
6What Not to Compress
Compression has a cost: CPU time and a little latency. For a 300-byte response, gzip's own overhead of headers and framing can make the result larger, and the time spent is wasted, so we set a minimum size, typically 1 KB (Tomcat calls it compressionMinSize, nginx gzip_min_length). Already compressed formats, JPEG, PNG, video, zip, and most PDFs, gain nothing and cost CPU, so compression should be limited to text types: application/json, text/html, text/csv, application/xml, JavaScript and CSS. Finally, responses that mix secrets with user-controlled input need care over HTTPS because of the BREACH attack.
7Follow-up questions
What is the difference between Content-Encoding and Transfer-Encoding?
Content-Encoding describes the representation itself, for example gzip, and is kept by caches and seen by the application. Transfer-Encoding, such as chunked, describes how the message is carried over one hop and is removed by the next hop. In HTTP/1.1, compression is practically always done with Content-Encoding.
Is Brotli better than gzip?
Brotli usually compresses text 15 to 25 percent smaller than gzip at similar decompression speed, and every modern browser supports it, so it is great for static assets and browser traffic at the proxy or CDN. For dynamic API responses, its higher levels cost much more CPU, so a moderate level or gzip is a common choice. Java support needs a library, which is another reason to do it at the proxy.
Does HTTP/2 make compression unnecessary?
No. HTTP/2 compresses headers with HPACK, but bodies are still sent as they are, so body compression with Content-Encoding is just as useful.
8Conclusion
In this answer, we saw that compression is a negotiation: Accept-Encoding from the client, Content-Encoding on the response, and Vary: Accept-Encoding so caches keep separate copies. In Jersey we enable it with EncodingFilter.enableFor and GZipEncoder, and in RESTEasy with its gzip support, resteasy.allowGzip and @GZIP, depending on the version. Often the reverse proxy or container is the better single place to compress, as long as we never compress twice, and we skip small bodies and formats that are already compressed.
Open with
Compression is negotiated. The client sends Accept-Encoding with the encodings it can decode, say gzip. The server may compress the body and then marks it with Content-Encoding gzip, while the Content-Type stays application/json.
Then, if they want more
And it must add Vary: Accept-Encoding, because otherwise a shared cache could hand a gzip body to a client that never asked for it; Vary makes the cache keep one copy per encoding. In Jersey I enable it in the ResourceConfig with EncodingFilter.enableFor, passing GZipEncoder, and optionally DeflateEncoder; the same setup also decodes gzip request bodies.
In RESTEasy there is a gzip interceptor that is enabled with the resteasy.allowGzip setting, and the @GZIP annotation forces it for a method, but the switches changed across versions so I would check the docs.
In practice, though, I often prefer to compress once at the reverse proxy, CDN or servlet container, because it is one setting for every service, it can use Brotli for browsers, and it keeps CPU off request threads, and I make sure we do not compress twice.
Wherever it runs, I set a minimum size around 1 KB and compress only text types like JSON, HTML and CSV, not images, zip files or PDFs, which are already compressed.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Jakarta EE specifications
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "How do you enable gzip compression of JAX-RS responses in Jersey and in RESTEasy, and...": https://scrivio-interview-library.netlify.app/java/jax-rs/2cf3989cf0/