Java

Change Spring Boot embedded container


This tutorial will show you show to change Spring Boot embedded container from its default Tomcat, to an alternative Jetty or Undertow.

The project Spring Boot Starter Web ships with Spring MVC and a Tomcat embedded container that at build time, is packaged within the executable jar file of the standalone Spring Boot application, so everything you need to run the application including the web container itself, is included in that jar.

However, you may want to change Tomcat for another container of your choice by whatever it is the reason, so I will going to show the required steps.

Exclude Tomcat from Starter Web project

Your current Spring Boot Starter Web dependency might look like the following in your pom.xml:

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
</dependency>

So first, you have to add an exclusion for the Tomcat that is shipped with it:

<!-- Spring Boot Starter Web MVC excluding Tomcat -->
<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
 <exclusions>
  <exclusion>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-tomcat</artifactId>
  </exclusion>
 </exclusions>
</dependency>

Now that you have excluded Tomcat, let’s move to how to add the new container.

Add Jetty dependency

All you need to do now is to add the Jetty dependency in your pom.xml like the following:

<!-- Jetty instead of Tomcat -->
<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

Or add Undertow dependency

However, if you prefer Undertow instead of Jetty and Tomcat, just add the Undertow dependency in your pom.xml file:

<!-- Undertow instead of Tomcat and Jetty -->
<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-undertow</artifactId>
</dependency>

Now, your new container is going to be used on the next startup:

I hope it helps.
Cya!

bgasparotto

View Comments

    • Hi Arilson. It sounds like your project hasn't updated its dependencies. If you perform a mvn clean install and try again, it should work.

  • Hi Bruno, congratulations! This is a short and very useful tip!!!

    Thank you!

Recent Posts

Python function decorator

This guide will show you how to create a Python function decorator with a few…

2 years ago

Got permission denied while trying to connect to the Docker daemon socket

This guide will show you how to fix the error Got permission denied while trying…

2 years ago

Python virtual environment on Intellij IDEA

This guide will show you how to create a Python virtual environment on Intellij IDEA…

2 years ago

Find and kill processes on Linux and Mac by port number

This tutorial will quickly show you how to to find and kill processes on Linux,…

2 years ago

Python: Relocation R_X86_64_PC32 against symbol can not be used when making a shared object Error

This guide shows a possible solution for Python error Relocation R_X86_64_PC32 against symbol can not…

2 years ago

Kubernetes useful commands

I condensed below a cheat sheet of Kubernetes useful commands. I will keep updating this…

2 years ago