
How to Enable GZIP Compression in Spring Boot?
HTTP compression is a capability that can be built into web servers and web clients to improve transfer. This post explains how to enable gzip compression in spring boot.
November 23, 2019 · 2 min read
HTTP compression is a capability that can be built into web servers and web clients to improve transfer speed and save bandwidth utilisation. The commonly used compression is GZIP.
By default, the gzip compression is disabled in the Spring Boot application. However, we can enable compression using a few property changes.
Enable GZIP Compression
Add the following configuration to your Spring Boot application.properties file to enable gzip response compression.
# Enable response compression
server.compression.enabled=true
# Mime types that should be compressed
server.compression.mime-types=text/xml, text/css, text/javascript, application/jsonThis configuration will enable the gzip compression for all responses for the given mime types defined in the property file.
💡
Please note, that the wildcard in mime types is not supported. So we need to provide the list of mime types explicitly.
The gzip operation consumes time and other server resources. You may enable the compression only when the response size exceeds a specific limit. This can be configured using the following property.
# Minimum response where compression will kick in
server.compression.min-response-size=4096Exclude user agents from the compression
You can also exclude the specific user agents using excluded-user-agents configuration.
server.compression.excluded-user-agents= Mozilla/5.0If you are using a YAML-based configuration, all the above properties can be written as follows:
server:
compression:
enabled: true
mime-types: text/xml, text/css, text/javascript, application/json
min-response-size: 1024
excluded-user-agents: Mozilla/5.0