čtvrtek 10. října 2019

Gzip HTTP Request with Spring Boot and Jetty

It is quite easy to get HTTP response gzipped with Spring Boot, all you need is to enable a property:

server.compression.enabled=true # response compression

The gzip encoding of HTTP request is quite different beast and it needs completely different approach.

First of all it is web-container specific - here is Spring configuration for Jetty using its GzipHandler:

@Component
public class JettyCustomizer implements
     WebServerFactoryCustomizer<ConfigurableJettyWebServerFactory> { 

  @Override
  public void customize(ConfigurableJettyWebServerFactory factory) {
    factory.addServerCustomizers(server -> {
        GzipHandler handler = new GzipHandler();
        handler.setInflateBufferSize(2_048);
        handler.setHandler(server.getHandler());
        server.setHandler(handler);
    });
  }
}


The Inflate Buffer Size has to be set and the handler is able to gzip responses as well.

The configuration can be tested using curl:

echo '{"foo":"bar"}' | gzip | curl --http1.1 -i -X POST http://localhost:8080/test -H "Content-type: application/json" -H "Content-Encoding: gzip" --data-binary @-

Žádné komentáře:

Okomentovat