How to enable http2 in spring boot application| HTTP Compression example

The following things need to be set up http2 in the spring boot application.

As you know latest browsers support http2 with SSL/TLS configuration Required

  • Spring boot
  • maven or Gradle

How to configure and enable http2 in spring boot application

Here is step by step to configure in spring boot in the 2.0 and 1.5 versions.

Create and configure HTTP SSL certificate

To set up http2, First, you need to configure the SSL configuration.

Get the certificate from providers or self-certificate I have the crt and key file which got from the provider.

openssl pkcs12 -export -out my_file.p12 -inkey my_key.key -in my_certificate.crt -password pass:secret

It creates a p12 file.

You can still skip the above step if you are creating a self-signed certificate which is a basic certificate.

First generate keystore file from p12 file

keytool -import -trustcacerts -alias server -file my_file.p7b -keystore my_keystore.jks

It asks for a password to input.

Once Keystore is generated, You have to configure http2 in the spring boot application.

In spring boot2.x version, It is simple to configure http2 in the spring boot application

Spring boot2.x has a built-in tomcat 9. x version which supports http2 by default.

Http2 supported since tomcat 8.5 version. You can check more about enable http2 in tomcat

In the application. properties or yaml file add below properties

server.http2.enabled=true
server.port = 8443
server.ssl.key-store=classpath:my_keystore.jks
server.ssl.key-store-password=secretpassword

And restart the application using

mvn spring-boot:run

In case, if you are using a Gradle wrapper, you can use the below. command

gradlew bootRun

It creates a default embedded tomcat server with http2 configured in the Java9 version.

Let’s see how to customize http2 with spring boot 1.5 and java 8 versions.

Configure http2 for spring boot 1.5.x version

This works with the java8 version. server.http2.enabled is not available in this version, SO you have to write a programmatically.

@Bean
public ConfigurableServletWebServerFactory tomcatCustomizer() {
    TomcatServletWebServerFactory tomcatWebserverFactory = new TomcatServletWebServerFactory();
    tomcatWebserverFactory.addConnectorCustomizers(connector -> connector.addUpgradeProtocol(new Http2Protocol()));
    return tomcatWebserverFactory;
}

How to configure HTTP Compression in spring boot application

By default embedded servers are not compressed.

Open application.properties to enable compression

server.compression.enabled=true

the response must be at least 2MB in size to compress the data, Still, you can configure it with server.compression.min-response-size with bytes

server.compression.min-response-size=4096

You can configure types of response to compression using `server.compression.mime-types

server.compression.mime-types

How do enable http2 for jetty server in spring boot application

first configure jetty dependencies in pom.xml for maven projects

  <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jetty</artifactId>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-alpn-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-alpn-conscrypt-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty.http2</groupId>
        <artifactId>http2-server</artifactId>
    </dependency>

You can also configure the same in Gradle projects.

Next configure

server.http2.enabled=true

It is configured by default jetty server with HTTP configuration.

Reference