Enterprise Java

Enabling HTTP/2 in Spring Boot with Tomcat

HTTP/2 is a major revision of the HTTP protocol that improves performance by enabling multiplexed streams, header compression, and server push. Spring Boot, which uses Tomcat as its default embedded server, supports HTTP/2 natively. This article explains how to enable HTTP/2 in a Spring Boot application running with Tomcat.

1. Setting Up a Test Project

To begin, create a new Spring Boot project using Spring Initializr or your preferred method. Include the following dependencies in pom.xml for a Maven project.

01
02
03
04
05
06
07
08
09
10
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
 
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

These dependencies provide support for building a web application using Spring Boot with an embedded Tomcat server.

2. Creating a Simple Controller for Testing

To test HTTP/2, create a basic REST controller.

1
2
3
4
5
6
7
8
@RestController
public class HelloController {
 
    @GetMapping("/hello")
    public String hello() {
        return "Hello, HTTP/2!";
    }
}

This controller exposes an endpoint (/hello) that returns a simple message.

Now, let’s use curl to check the HTTP version being used before enabling a self-signed certificate. Running the following command will send a request to the local server and display the response headers:

1
curl -sI http://127.0.0.1:8080/hello

This command retrieves only the headers (-I flag) from the response. If the server is running correctly, you should see output similar to the following:

Spring Boot Tomcat example showing output with HTTP/1.1 enabled.

The first line indicates that the server is currently using HTTP/1.1, meaning HTTP/2 is not yet enabled. HTTP/2 requires HTTPS to function properly. Next, let’s configure the application to enable HTTP/2 and verify the changes.

3. Generating a Self-Signed SSL Certificate

Since HTTP/2 requires HTTPS, we need an SSL certificate. For development and testing purposes, we can generate a self-signed certificate using the Java keytool command.

1
keytool -genkeypair -alias tomcat -keyalg RSA -keysize 2048 -storetype PKCS12 -keystore keystore.p12 -validity 3650

This command creates a keystore.p12 file that can be used in the configuration. Place this file in src/main/resources/.

4. Enabling HTTP/2 in Spring Boot

To enable HTTP/2 in a Spring Boot application running with Tomcat, we need to update the server configuration. Since Tomcat only supports HTTP/2 over HTTPS, we must also configure an SSL certificate. This can be done in the application.yml or application.properties file by specifying the HTTP/2 protocol and providing SSL settings. The following configuration ensures that HTTP/2 is enabled when the application runs.

Using application.properties

1
2
3
4
5
server.port=8443
server.http2.enabled=true
server.ssl.key-store=classpath:keystore.p12
server.ssl.key-store-password=changeit
server.ssl.key-store-type=PKCS12

These configurations enable HTTP/2 over HTTPS on port 8443. Since Tomcat requires HTTPS for HTTP/2, we specify an SSL key store. The key store file keystore.p12 must be placed in the resources folder.

5. Running and Testing HTTP/2 Support

Start the application and test if HTTP/2 is working using curl:

1
curl -k -I https://127.0.0.1:8443/hello

This command sends an HTTPS request to https://127.0.0.1:8443/hello, retrieving only the response headers while ignoring SSL certificate validation errors. The -k option allows insecure SSL connections, which is useful when using a self-signed certificate.

If HTTP/2 is enabled, you should see the output containing:

1
2
3
4
HTTP/2 200
content-type: text/plain;charset=UTF-8
content-length: 14
date: Mon, 31 Mar 2025 09:55:55 GMT

This confirms that the application is serving requests over HTTP/2. You can also set up a few more things, like redirecting HTTP to HTTPS in Tomcat and using Spring Security to make sure all connections use SSL.

5. Conclusion

In this article, we explored how to enable HTTP/2 in a Spring Boot application using Tomcat. We covered the necessary configurations, including enabling HTTP/2 in application.properties, setting up an SSL certificate, and testing the setup using curl. Additionally, we can enhance security by redirecting HTTP to HTTPS and integrating Spring Security to enforce SSL connections.

6. Download the Source Code

This article covered HTTP2 support in Spring Boot with Tomcat.

Download
You can download the full source code of this example here: spring boot http2 tomcat

Omozegie Aziegbe

Omos Aziegbe is a technical writer and web/application developer with a BSc in Computer Science and Software Engineering from the University of Bedfordshire. Specializing in Java enterprise applications with the Jakarta EE framework, Omos also works with HTML5, CSS, and JavaScript for web development. As a freelance web developer, Omos combines technical expertise with research and writing on topics such as software engineering, programming, web application development, computer science, and technology.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button