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 or yaml file add below properties

server.port = 8443

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.

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 to enable compression


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


You can configure types of response to compression using 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


You can also configure same in gradle projects

Next configure


This configure by default jetty server with http configuration


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
You'll get a notification every time a post gets published here.