SoFunction
Updated on 2025-04-14

Technical Guide to Implementing Gzip Compression Optimization in SpringBoot3

1. Brief description

As the number of users and data of web applications increases, network bandwidth and page loading speed have gradually become bottlenecks. To reduce the amount of data transmission and improve the user experience, we can use Gzip to compress HTTP responses. This article will introduce how to implement Gzip compression optimization in Spring Boot 3.

2. Configuration

Spring Boot 3 provides out-of-the-box support for Gzip compression, and we can enable Gzip compression through simple configuration.

2.1 Add dependencies

In Spring Boot 3 projects, no additional dependencies are required, as Gzip support is built-in. Just configure it.

2.2 Configuring Gzip compression

Gzip compression can be easily enabled by modifying or file.

Configuration example:

server:
  compression:
    enabled: true
    mime-types: text/html, text/xml, text/plain, text/css, text/javascript, application/javascript, application/json, application/xml
    min-response-size: 1024

Configuration example:

=true
-types=text/html,text/xml,text/plain,text/css,text/javascript,application/javascript,application/json,application/xml
-response-size=1024

Configuration details:

  • : Enable or disable Gzip compression. Set to true to enable compression.
  • -types: Define which MIME types of response data will be compressed. Usually we compress HTML, CSS, JavaScript, JSON and other types of data.
  • -response-size: Sets the minimum response size (in bytes) of compression. If the response data is less than this value, no compression will be performed. Set to 1024 means that compression will only occur if the response is greater than 1KB.

3. Server-side application

In Spring Boot 3, enabling Gzip compression is very simple. Here is an example of how to implement Gzip compression in a backend service to optimize API response data. Suppose there is an API that returns JSON data:

@RestController
public class DataController {

    @GetMapping("/data")
    public ResponseEntity<List<String>> getData() {
        List<String> data = ("Item 1", "Item 2", "Item 3", "Item 4", "Item 5");
        return (data);
    }
}

When Gzip compression is enabled, this API response will be compressed to reduce the amount of data transmitted when the amount of data is large.

You can use curl to see if the backend has Gzip compression enabled:

curl -H "Accept-Encoding: gzip" -I http://localhost:8080/data

You will see something similar in the response header:

Content-Encoding: gzip

This indicates that Gzip compression has taken effect.

4. Front-end application

In the front end, Gzip is often used to compress static resources (such as HTML, CSS, JavaScript files), thereby reducing the time it takes for users to load pages. Typically, these static resources are Gzip-compressed via a web server (such as Nginx or Apache) or a packaging tool (such as Webpack).

4.1 Nginx configures Gzip compression for static resources

Assuming you are using Nginx as a static resource server in your front-end application, you can enable Gzip compression by modifying the Nginx configuration. Nginx configuration file () example:

server {
    listen 80;
    server_name ;

    # Enable gzip compression    gzip on;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
    gzip_min_length 1024;
    gzip_comp_level 5;

    location / {
        root /var/www/html;
        try_files $uri $uri/ =404;
    }
}
  • gzip on: Enable Gzip compression.
  • gzip_types: Specifies the MIME type to compress (HTML, CSS, JavaScript, JSON, etc.).
  • gzip_min_length: compress only files larger than the specified length (1024 bytes).
  • gzip_comp_level: Compression level (1-9), the larger the number, the stronger the compression, but at the same time, the CPU resource consumption is more.

4.2 Gzip compression of front-end files using Webpack

If you use the Webpack build tool in front-end development, you can use compression-webpack-plugin to Gzip compress the generated static resources.

Install plug-in:

npm install compression-webpack-plugin --save-dev

Revise :

const CompressionPlugin = require('compression-webpack-plugin');

 = {
    entry: './src/',
    output: {
        path: __dirname + '/dist',
        filename: ''
    },
    plugins: [
        new CompressionPlugin({
            filename: '[path].gz[query]',
            algorithm: 'gzip',
            test: /\.(js|css|html|svg)$/,
            threshold: 10240,
            minRatio: 0.8,
        }),
    ],
};
  • algorithm: ‘gzip’: Use the Gzip algorithm.
  • test: /.(js|css|html|svg)$/: Specifies the file type to be compressed.
  • threshold: 10240: Only compress files with sizes exceeding 10KB.
  • minRatio: 0.8: Minimum compression ratio, only files with compression ratio less than 0.8 will be compressed.

After running the Webpack build, the static resources will generate a compressed version of Gzip. The server can provide these compressed files to the browser first, reducing loading time.

5. Performance improvement

After using Gzip compression, the amount of response data can be significantly reduced, especially when the amount of data is large, the performance improvement is particularly obvious. Here are some examples of optimization results:

  • Before compression: The original JSON response size is 120KB.
  • After compression: With Gzip compression enabled, the response data size is reduced to 30KB.
    By reducing the size of transmitted data, the application's response speed is greatly improved and bandwidth consumption is reduced.

Things to note

  • CPU overhead: Although Gzip compression can reduce the size of transmitted data, the compression and decompression process will consume a certain amount of CPU resources. If your application is CPU sensitive, it is recommended to set min-response-size reasonably to avoid compressing small files.
  • Static resources: For static resources (such as CSS and JavaScript files), if you use Nginx or other proxy servers, it is recommended to perform Gzip compression at the proxy layer rather than in Spring Boot.

6. Conclusion

Enabling Gzip compression in Spring Boot 3 is an easy and effective way to optimize application performance, especially for applications that need to handle large amounts of static resources and API responses. Through reasonable configuration, you can significantly reduce network bandwidth consumption and improve application response speed, thus providing users with a better experience.

The above is the detailed content of the technical guide for implementing Gzip compression optimization in SpringBoot3. For more information about SpringBoot3 Gzip compression optimization, please follow my other related articles!