Spring Boot REST – Response Gzip Compression

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 both application/vnd.company.v3+json and application/vnd.company.v2+xml. [Link]

Drop me your questions related to Gzip compression in spring boot.

Happy Learning !!

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.