Friday 13 September 2019

Spring boot: Compress response messages


You can compress response messages by adding below properties in application.properties file.

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

By default, responses must be at least 2048 bytes in length for compression to be performed. This can be configured using the server.compression.min-response-size property.

Find the below working application.


DummyPayloadController.java
package com.sample.app.controller;

import java.security.SecureRandom;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DummyPayloadController {
    private static final String ALPHA_NUMERICALS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    private static SecureRandom rnd = new SecureRandom();
    private static final int LENGTH = ALPHA_NUMERICALS.length();

    @RequestMapping("/randomData")
    public String dummyData() {
        return randomString(10000);
    }

    public static String randomString(int len) {
        final StringBuilder builder = new StringBuilder(len);

        for (int i = 0; i < len; i++)
            builder.append(ALPHA_NUMERICALS.charAt(rnd.nextInt(LENGTH)));

        return builder.toString();
    }

}


App.java
package com.sample.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
    
    
}


application.properties
server.compression.enabled=true
server.compression.min-response-size=1024
server.compression.mime-types=application/json,application/xml,text/html,text/xml,text/plain


pom.xml
<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>springbootMisc</groupId>
    <artifactId>springbootMisc</artifactId>
    <version>1</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
    </parent>

    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>

Run App.java.


Open the url ‘http://localhost:8080/randomData’ and inspect response headers, you can observe gzip encoding is enabled.

You can download complete working application from this link.


Previous                                                    Next                                                    Home

No comments:

Post a Comment