How to Compress JSON Response in Spring Boot

In modern web development, it’s common to encounter scenarios where the size of the JSON response from a Spring Boot application needs to be minimized for improved performance and bandwidth utilization. Compressing JSON responses can greatly reduce their size, allowing for faster and more efficient transmission over the network. In this article, we will explore various techniques and approaches to achieve JSON response compression in a Spring Boot application. By implementing these techniques, developers can optimize their applications to deliver smaller, compacted JSON responses, resulting in improved performance and an enhanced user experience.

What Is the Default Compression in Spring Boot?

By default, compression is disabled in Spring Boot. Compression is a technique used to reduce the size of files or data in order to improve transmission speed and save storage space. In the context of a Spring Boot application, compression refers to the compression of HTTP responses sent by the application to clients.

This can greatly improve performance, especially for applications that transfer large amounts of data over the network.

There are different compression algorithms that can be used, such as Gzip and Deflate. Gzip is the most commonly used algorithm, as it provides good compression ratios and is widely supported by web browsers and web servers.

Additionally, they can specify the minimum content length for compression using the “server.compression.min-response-size” property. By default, Spring Boot compresses responses larger than 2KB.

It’s worth noting that compression comes with a small overhead in terms of CPU usage, as the application needs to compress the response content. However, the benefits of compression, such as faster transmission speed and reduced bandwidth consumption, generally outweigh the overhead.

It’s important to consider the trade-offs, such as the CPU overhead required for compression, when deciding whether to enable compression in a Spring Boot application.

Let’s explore how to reduce the size of JSON responses by compressing them with gzip. JSON data compresses well as text data, making gzip an ideal solution. It can be seamlessly integrated into the HTTP protocol, which is commonly used for transmitting JSON data.

How to Reduce the Size of JSON Response?

Gzip compression reduces the size of JSON responses by compressing the data before sending it over the network. This is done by replacing repetitive sequences of characters with shorter codes.

To implement gzip compression, you need to configure your web server to enable gzip compression for JSON responses. This can typically be done by modifying the server configuration file or using a plugin or module specific to your web server.

When a client requests JSON data from your server, the server checks if the client supports gzip encoding.

This process is usually handled automatically by the client, so you don’t have to explicitly decompress the data yourself.

This is especially important for mobile devices or network-constrained environments where bandwidth is limited.

These include deflate compression, which is another common compression algorithm, and brotli compression, which is a newer compression algorithm that offers even better compression ratios than gzip.

By enabling gzip compression on your web server, you can minimize the amount of data that needs to be transmitted over the network, resulting in faster and more efficient data transfer.

Spring Boot provides a configuration option called min-response-size, which allows you to define the minimum number of bytes in a response for compression to be performed. By default, this size is set to 2048 bytes. This configuration is useful in optimizing network bandwidth and reducing data transfer time by compressing responses that exceed the defined size threshold.

What Is the Minimum Response Size for Spring Boot Compression?

The minimum response size for Spring Boot compression is determined by the configuration property called “min-response-size”. This property allows you to specify the minimum number of bytes in a response for compression to be performed. By default, the minimum response size is set to 2048 bytes.

Setting the minimum response size is important for optimizing the performance of your application. By compressing larger responses, you can reduce the amount of data that needs to be transmitted over the network, resulting in faster response times and lower bandwidth usage.

The default size of 2048 bytes is a reasonable starting point, but you may need to adjust this value based on the characteristics of your application and the nature of the responses it generates. If your application tends to generate smaller responses, you could consider lowering the minimum response size to enable compression for a wider range of responses.

Source: Spring Boot REST – Response Gzip Compression


Spring Boot provides convenient methods for enabling compression, such as configuring the compression properties in the file or using annotations in the controller classes. Additionally, by considering factors like content negotiation, caching headers, and profiling, developers can further optimize the compression process to suit specific application requirements.

Scroll to Top