Let’s learn how to change the default tomcat server with a jetty server in Spring boot with detailed instructions.
Tomcat and Jetty are the most known web servers in Java. They both have identical features and have the same set of features to offer. Even though I personally prefer tomcat, there are ways to update your spring boot application to use Jetty.
To replace tomcat with jetty, you need to follow these steps.
- Remove tomcat starter from spring-boot-starter-web
- Add jetty starter
- Adjust application.properties for any server.tomcat.* entries( if available ).
To remove the tomcat starter from your project, simply add an exclusion to the web dependency. And to add jetty support, just add the org.springframework.boot:spring-boot-starter-jetty dependency.
<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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency>Code language: HTML, XML (xml)
Even though jetty and tomcat do the same thing, under the hood they may be written/configured differently. For this reason, the configuration that you have placed for the tomcat may not work for the jetty.
For example, the below snippets set the maximum and the minimum number of threads for handling the web requests.
#Sample server Configuration for TOMCAT =400 =400 =100Code language: Properties (properties)
#Sample server Configuration for JETTY =400 =10 =100Code language: Properties (properties)
As you can see, the settings don’t match the exact names. But they are equivalent and you should handle these on switching between tomcat and jetty. But it is most likely that you never configured tomcat.
With the above completed, let us start our application.
As you can see, our application starts using Jetty as the embedded server.