Spring Boot Response Compression and Client gzip Requests — Which Body Gets Smaller?
On a request, Accept-Encoding: gzip tells the server the client can receive a gzipped response. To gzip a request body, the client compresses the JSON bytes and adds Content-Encoding: gzip. Spring Boot’s server.compression.enabled applies to responses the server sends. RFC 9110 ` 12.5.3 · Spring Boot response compression
| Goal | Request header | Who compresses the body? |
|---|---|---|
| Receive a smaller product listing | Accept-Encoding: gzip |
The server compresses the response and adds Content-Encoding: gzip to it |
| Send a smaller batch of events to a collector | Content-Encoding: gzip |
The client compresses the request and the receiving side decompresses it |
Both headers can go on the same request. A client can gzip its events and also tell the server it accepts a gzipped response. Content-Type: application/json describes the data format to read after decompression. RFC 9110 ` 8.4
In both flows, Content-Encoding: gzip accompanies the JSON body that has already been compressed. The response flow shows what happens when the server’s compression conditions are met.
Enable Spring Boot response compression
Add the following settings to application.yml. Spring Boot documents a default minimum response size of 2 KB (2,048 bytes) for compression. This example selects application/json only. Add the appropriate entries to mime-types if the service should also compress HTML or CSS. Spring Boot response compression
server:
compression:
enabled: true
min-response-size: 2KB
mime-types: application/json
Assume /api/products returns a fixed JSON response larger than 2 KB on your local server. With Bash and curl, grep, wc, gzip, and cmp available, make one request with identity and one with gzip to compare the response headers and body sizes.
url=http://localhost:8080/api/products
curl -fsS -D plain.headers -o response.json \
-H 'Accept-Encoding: identity' "$url"
curl -fsS -D gzip.headers -o response.gz \
-H 'Accept-Encoding: gzip' "$url"
grep -i '^content-encoding:' gzip.headers
wc -c response.json response.gz
gzip -dc response.gz | cmp response.json -
When gzip is applied, gzip.headers shows Content-Encoding: gzip. The final cmp succeeds if the decompressed body matches the original. These commands save the body in its compressed form for the size comparison. With curl --compressed, curl decompresses the response body automatically, so the saved file size differs from the compressed body size sent over HTTP. The saved headers can still show Content-Encoding: gzip. curl --compressed documentation
Send a gzipped request from the client
Clients can compress JSON-heavy requests such as event batches. This example assumes your local /api/events explicitly supports gzipped request bodies. --data-binary sends the compressed bytes as they are. curl --data-binary documentation
printf '%s' '{"events":[{"type":"page_view","page":"/products"}]}' > events.json
gzip -c events.json > events.json.gz
curl -i http://localhost:8080/api/events \
-H 'Content-Type: application/json' \
-H 'Content-Encoding: gzip' \
--data-binary @events.json.gz
The receiving side must decompress gzip before parsing JSON. If a gateway does the decoding, it forwards the decoded body and handles Content-Encoding and Content-Length to match the forwarded body. If the application decodes it, decide which endpoints support gzip and set separate limits for compressed and decompressed size. When rejecting an unsupported content coding, the server can return 415 Unsupported Media Type and use the response’s Accept-Encoding to indicate the codings it accepts. RFC 9110 ` 12.5.3
What to check in production
- Response compression. Compare the direct response from the application with the response along the real user path. A CDN or proxy might handle compression. Where caching is enabled, check
Vary: Accept-Encodingand the cache configuration together. RFC 9110 ` 12.5.5 - Request compression. Send the same JSON uncompressed and with gzip, then check that both produce the same application outcome. Verify that corrupted gzip or requests exceeding the decompressed size limit are rejected before data is saved.
- Compression results. Measure body size, processing time, and CPU usage with representative data. In a local Python 3 round trip, repeating the
page_viewevent above 100 times, serializing withjson.dumps(..., separators=(',', ':')), and compressing withgzip.compress(..., mtime=0)reduced the body from 4,012 bytes to 97 bytes. The decompressed bytes matched the original. This result comes from a highly repetitive sample. The HTTP path through Spring Boot and a proxy was not tested.
For a comparison of HTTP clients in Spring, see the companion guide. WebClient vs. RestClient
Discussion