{

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

configure http2 in spring boot application

Here are step by step to configure in spring boot in 2.0 and 1.5 version

Create and configure HTTP SSL certificate

To setup http2, First, you need to configure SSL configuration.

Get the certificate from providers or self-certificate I have 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.

For spring boot 1.5.x

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 Compression HTTP 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 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

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.