If the responses of your service are larger, you can let the service compress the body of the response in order to save bandwith and gain transmission speed. This is escpecially important if you have to pay for bandwith.
We assume that you have already defined a dependency on the starter web module:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Now you can enable compression in the application.yml
file:
server:
compression:
enabled: true
min-response-size: 1024
mime-types: text/plain,text/html,text/css,application/json
The attribute min-response-size
defines the threshold for the compression.
If the payload is larger than the threshold, the response body is
compressed. Additionally you can also define the MIME types
for which the configuration is applied.
In case you don’t want to the enable compression for all responses, Spring offers a dedicated annotation for methods and classes:
@RequestMapping("/cmprss")
@EnableCompression
public String cmprss() {
return "Compress me.";
}