Spring Boot - Replace Tomcat With Jetty As the Embedded Server01 Sep 2019
Apache Tomcat and Eclipse Jetty are popular web servers and javax.servlet containers for Java. Many people prefer Tomcat (standalone) in production and its widely used compared to Jetty. On the other hand, Jetty is light-weight and has a smaller CPU and memory footprint. I have used Jetty in production for high-throughput REST API applications and its performance has been fantastic.
Many developers prefer to use Jetty when developing and testing on their local machines. Spring Boot web starter uses Tomcat as the default embedded server. I frequently find myself changing from Tomcat to Jetty on my Spring Boot projects.
In this post, we’ll take a look at steps to replace Tomcat with Jetty in Spring Boot projects.
If you’d like to change the embedded web server to Jetty instead, you’ll have to:
- Exclude Tomcat from web starter dependency, since it is added by default
- Add the Jetty dependency
Step 1: Exclude Tomcat
Find the following dependency in
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
Replace it with:
<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>
Step 2: Add Jetty
Add the following dependency to your
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency>
Please note that there are some other starters e.g. ThymeLeaf templating engine, etc. that might pull in Tomcat by default. If you’re using one of these, you’ll have to manually exclude Tomcat from all such dependencies. When I’m running into issues, I look at maven dependencies to check if Tomcat is being pulled in. You could check for this by either inspecting the dependencies on command line e.g.
mvn dependency:tree -Dverbose or using the inspector in your favourite IDE e.g. IntelliJ or Eclipse.
That’s all. Hope you found this post useful. Please leave comments below and share.