Spring Boot - Enable HTTP Compression in Tomcat

Mar 01, 2020 · 1 min read

Spring Boot web applications use embedded Tomcat as the webserver.

Compression is a simple yet effective way to improve the performance and response times of your website or web application and save bandwidth. By default, compression is disabled. In this tutorial, we’ll see how to enable GZIP compression for Tomcat.

  1. Enable HTTP Compression in Tomcat
  2. Set Minimum Tomcat Compression Length
  3. Set MIME Types for Tomcat Compression

Enable HTTP Compression in Tomcat

You can easily enable HTTP compression using GZIP for Tomcat by setting the server.tomcat.compression: on property in application.properties or application.yml file.

In your application.properties file,

/src/main/resources/application.properties

server.tomcat.compression: on

If you use YAML file for your cofiguration,

/src/main/resources/application.yml

server:
  tomcat:
    compression: on

Set Minimum Tomcat Compression Length

By default, Tomcat will compress responses with a length of at least 2048 bytes or 2 kilobytes. This limit can be configured by setting the server.tomcat.compression an integer value indicating the desired amount in bytes, rather than on. In the example below, let’s set the minimum response length for compression.

/src/main/resources/application.properties

server.tomcat.compression: 4096

If you use YAML file for your cofiguration,

/src/main/resources/application.yml

server:
  tomcat:
    compression: 4096

Set MIME Types for Tomcat Compression

By default, Tomcat will only compress responses text/html, text/xml, and text/plain MIME types. You can configure this using the server.tomcat.compressableMimeTypes property. For example:

/src/main/resources/application.properties

server.tomcat.compressableMimeTypes=application/json,application/xml

If you use YAML file for your cofiguration,

/src/main/resources/application.yml

server:
  tomcat:
    compressableMimeTypes=application/json,application/xml

That’s all. If you found this tutorial useful, we request that you share it with your followers using the social media buttons below. It will help CodeAhoy grow. Thank you.

#java #springboot #tomcat

You May Also Enjoy


If you like this post, please share using the buttons above. It will help CodeAhoy grow and add new content. Thank you!


Comments (1)


SS

Hi, the conguration mentioned did not work in my case. Am working with Spring boot 2.2.8 and below is my yaml:

server: tomcat: compression: 1024 compressableMimeTypes: application/json,application/xml


Speak Your Mind