1. Overview

Spring boot response compress or gzip can be enabled using  server.compression.enabled in application.properties file, It will be supported for Tomcat, Jetty, and Undertow servers. While compress response, accept-encoding response header will be set to gzip.

application.properties

server.compression.enabled=true
server.compression.min-response-size=1

server.compression.min-response-size indicate the minimum size of the response to compress or gzip, By default, responses must be at least 2048 bytes in length for compression to be performed.

By default, responses will only be compressed if their content type is one of the following:

  • text/html
  • text/xml
  • text/plain
  • text/css

Other content types can be configured using server.compression.mime-types in application.properties file like

server.compression.mime-types=application/pdf,text/html

2. Example:

 

Spring boot response compress example

Spring boot response compress example

2.1 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>spring-boot-example</groupId>
    <artifactId>spring-boot-response-compress</artifactId>
    <version>1.0-SNAPSHOT</version>
    <description>Spring boot response compress for Tomcat, Jetty, and Undertow servers</description>
    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
    </parent>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <!-- Package as an executable jar -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2.1 application.properties

server.compression.min-response-size in bytes which indicate only those content which size is grated then this value.

server.compression.enabled=true
server.compression.min-response-size=1

2.2 CompressDemoController

package com.javadeveloperzone.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * Created by Java Developer Zone on 19-07-2017.
 */
@RestController
public class CompressDemoController {
    @RequestMapping(value = "/demo")
    public String demo() {
        return "Spring boot compressed response.";
    }
}

2.3 SpringBootConfig

package com.javadeveloperzone;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
/**
 * Created by JavaDeveloperZone on 19-07-2017.
 */
@SpringBootApplication
@ComponentScan // Using a root package also allows the @ComponentScan annotation to be used without needing to specify a basePackage attribute
public class SpringBootConfig {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootConfig.class, args);            // it wil start application
    }
}

2.4 Output

 

Spring boot response compress - output

Spring boot response compress – output

 

4. References

 

Was this post helpful?

1 comment. Leave new

I am getting following error while setting
server.compression.min-response-size=1 # in bytes

Field error in object ‘server’ on field ‘compression.minResponseSize’: rejected value [1 # in bytes]; codes [typeMismatch.server.compression.minResponseSize,typeMismatch.compression.minResponseSize,typeMismatch.minResponseSize,typeMismatch.int,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [server.compression.minResponseSize,compression.minResponseSize]; arguments []; default message [compression.minResponseSize]]; default message [Failed to convert property value of type [java.lang.String] to required type [int] for property ‘compression.minResponseSize’; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [int]]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133) ~[spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
at com.argusoft.triagestat.config.Application.main(Application.java:85) [classes/:na]

Leave a Reply

Your email address will not be published. Required fields are marked *