How to enable GZIP compression in Spring Boot application?

June 27, 2019 No comments gzip compression QA Spring Boot

Compressing static resources is a great way to save bandwidth and improve user experience. This is also the easiest way to speed up page loading which affects SEO in a positive way. The best and commonly used compression is GZIP. This kind of compression is disabled in Spring Boot by default but fortunately, we can enable it by setting below parameters in application.properties file.

# Whether response compression is enabled.
server.compression.enabled=true

# List of user-agents to exclude from compression.
server.compression.excluded-user-agents= 

# Comma-separated list of MIME types that should be compressed. Default mime-types are: text/html, text/xml, text/plain, text/css
server.compression.mime-types=text/html,text/xml,text/plain,text/css,text/javascript,application/javascript 

# Minimum "Content-Length" value that is required for compression to be performed.
server.compression.min-response-size=2048

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

server:
    compression:
        enabled: true
        excluded-user-agents: 
        mime-types: text/html,text/xml,text/plain,text/css,text/javascript,application/javascript 
        min-response-size: 2048


To find out if everything was correctly configured, check response headers for files choosed for compression. There should be Content-Encoding header with gzip value, also these files should be definitely smaller then before compression. How to enable gzip compression in spring boot

{{ message }}

{{ 'Comments are closed.' | trans }}