0

I am working in a Spring Boot (v 1.2) and AngularJS application. I am trying to implement GZip compression in our application to improve performance.

I want to compress my JSON response. What i did already...

1.Enabled compression in tomcat (spring boot embedded) as below. References: Stackoverflow... Using GZIP compression with Spring Boot/MVC/JavaConfig with RESTful and spring docs... https://docs.spring.io/spring-boot/docs/current/reference/html/howto-embedded-servlet-containers.html#how-to-enable-http-response-compression

@Bean
public EmbeddedServletContainerCustomizer servletContainerCustomizer() {
    return new EmbeddedServletContainerCustomizer() {
        @Override
        public void customize(ConfigurableEmbeddedServletContainer servletContainer) {
            ((TomcatEmbeddedServletContainerFactory) servletContainer).addConnectorCustomizers(
                    new TomcatConnectorCustomizer() {
                        @Override
                        public void customize(Connector connector) {
                            AbstractHttp11Protocol httpProtocol = (AbstractHttp11Protocol) connector.getProtocolHandler();
                            httpProtocol.setCompression("on");
                            httpProtocol.setCompressionMinSize(256);
                            String mimeTypes = httpProtocol.getCompressableMimeTypes();
                            String mimeTypesWithJson = mimeTypes + "," + MediaType.APPLICATION_JSON_VALUE;
                            httpProtocol.setCompressableMimeTypes(mimeTypesWithJson);
                        }
                    }
            );
        }
    };
}

Now, content-encoding:gzip is missing in my response header. So the above code has no impact, as of now. My questions are... 1. How to set "content-encoding:gzip" in spring? -> I tried inside spring controller like httpservletresponse.addHeader("content-encoding","gzip")

But, this time, the responsebody is empty at the browser (after adding..httpservletresponse.addHeader("content-encoding","gzip"))

  1. In addition to the above, still something pending? --> explicitly compress the response using java.util.zip.GZIPOutputStream or when I enable compression like httpProtocol.setCompression("on"); this is automatically taken care by spring boot ? --> create a servlet filter is required?

bottom line is : I am not clear between the "plain servlet way of implementation" and "spring boot" way of implementation.

In Spring Boot context, Could anyone list the steps required and NOT required?

Community
  • 1
  • 1
Sundararaj Govindasamy
  • 8,180
  • 5
  • 44
  • 77

1 Answers1

0

I did not test in Spring Boot 1.2, but in Spring Boot 1.3 to enable compress for REST responces you need to enable compression in application.properties:

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

After that all cacheable (not POST, PUT, etc) request, bigger than server.compression.min-response-size, will be compressed.

P.S.

In response Using GZIP compression with Spring Boot/MVC/JavaConfig with RESTful exists configuration for enabling compression in Spring Boot 1.2:

server.tomcat.compression: on
server.tomcat.compressableMimeTypes=application/json,application/xml,text/html,text/xml,text/plain
Community
  • 1
  • 1