23 June 2017

Spring Boot response compression

If Spring Boot app produces response (like json, xml) which is huge in size, gzip compression is a simple way to reduce latency and bandwidth. Gzip is used for file compression and decompression. These days processing time is cheap (multi-core CPU's are commonplace) while bandwidth is expensive.

If the SpringBoot app is using embedded tomcat, gzip can be enabled using simple configurations

In case of application.properties include:

server.compression.enabled=true
server.compression.mime-types=application/json,application/xml,text/html,text/xml,text/plain

In case of application.yml include:

 server: 
   compression: 
     enabled: true
     mime-types: application/json

For compressed response to work, clients calling the endpoint has to accept gzip encoded response. Clients using Spring RestTemplate need not do anything specific, RestTemplate supports gzip compression by default. In case of curl following command can be used to check compressed response size.

curl --compressed -H "content-type:application/json" -X POST URL -w "size: %{size_download}\n" 
Note: Compressed HTTP header request compressed content, if the server can provide it]


I have tried above configuration in SpringBoot v1.3.5.RELEASE

No comments: