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


The following things to need to setup http2 in spring boot application

As you know latest browsers supports 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 provider.

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

This creates a p12 file

You can still skip the above step if you are creating self sign certificate which is 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 password to input

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

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

Spring boot2.x has 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

Incase, if you are using Gradle wrapper, you can use below command

gradlew bootRun

This creates a default embedded tomcat server with http2 configured in Java9 version

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

For spring boot 1.5.x

This works with 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

response must be atleast 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 same in gradle projects

Next configure

server.http2.enabled=true

This configure 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.