StackTips

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 speed and save bandwidth utilization.

The commonly used compression is GZIP.

By default, the gzip compression is disabled in Spring Boot application. However, we can enable compression using the few property changes.

Spring Boot Configuration to enable GZIP Compression

Add the following configuration to spring boot application.properties file.

# Enable response compression
server.compression.enabled=true
 
# Mime types that should be compressed
server.compression.mime-types=text/xml, text/css, text/javascript, application/json

The above configuration will enable the gzip compression for all responses for the given mime-types defined in the property file.

The wildcard in mime-types are not supported. So we need to explicitly provide all the list of mime-types.

Please note, gzip operation consumes time and a lot of server resources. For this, you may enable the compression-only when the response exceeds a limit.

# Minimum response where compression will kick in
server.compression.min-response-size=4096

Exclude user-agents from compression.

server.compression.excluded-user-agents= Mozilla/5.0

If you are using YAML configuration file, these properties will look like this:

server:
  compression:
    enabled: true
    mime-types: text/xml, text/css, text/javascript, application/json
    min-response-size: 1024
    excluded-user-agents: Mozilla/5.0

Read More:

Enable HTTP Response Compression
User-Agent

nilanchala avtar

Nilanchala Panigrahy

A blogger, a bit of tech freak and a software developer. He is a thought leader in the fusion of design and mobile technologies. He is the author of Xamarin Mobile Application Development for Android Book (goo.gl/qUZ0XV3)