Learn to enable HTTP response gzip compression in spring boot 2 applications. The configuration shown in this tutorial can be applied to image compression as well as text compression, both.
By default, compression is disabled in spring boot.
1. Gzip Compression Configuration
The given below response compression properties are applicable to Jetty, Tomcat, and Undertow. Define these properties into application.properties
file.
# Enable response compression server.compression.enabled=true # Minimum response where compression will kick in server.compression.min-response-size=4096 # Mime types that should be compressed server.compression.mime-types=text/html, text/xml, text/plain, text/css, text/javascript, application/javascript, application/json
- server.compression.enabled enables or disables the compression.
- server.compression.min-response-size – configures the minimum number of bytes in response for compression to be performed. Default size is
2048
bytes. - server.compression.mime-types – enables compressed only if their content type is one of the given mime-type.
Read More : Enable HTTP Response Compression
2. Wildcards in mime-type are not supported
The compression, spring boot relies on support provided by underlying embedded servers. Unfortunately, these servers do not provide any consistent wildcards support in mime-type.
- Jetty is doing strict string comparisons, but it also allows configuring a whitelist/blacklist for this. [Link]
- Tomcat is checking that the response content-type starts with one of the configured mimetypes. So configuring
application/vnd.company
would work for bothapplication/vnd.company.v3+json
andapplication/vnd.company.v2+xml
. [Link]
Drop me your questions related to Gzip compression in spring boot.
Happy Learning !!