Card 04/ 06

GotchaDifficulty: Advanced1 min

Swapping the Embedded Server From Tomcat to Jetty

Adding the Jetty starter alone changes nothing, because Tomcat is still there and still wins.

spring-boot-starter-web brings in Tomcat as its embedded server. To run on Jetty instead, you have to exclude Tomcat from that starter as well as add spring-boot-starter-jetty; adding Jetty on its own leaves the application running on Tomcat.

The symptom is a project that declares the Jetty starter, logs that Tomcat started, and behaves exactly as it did without the Jetty starter. Both servers are now on the classpath, and Boot's server configuration checks for Tomcat first and stops once it has found it.

Nothing fails, which is what makes it costly: the application runs on the wrong server, and any Jetty-specific settings you added are silently ignored.

The fix is an exclusion inside the web starter's entry, and the Jetty starter beside it:

text
<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>
text
Server: JettyWebServer
run in a container, with Tomcat excluded

With Tomcat excluded, Jetty is the only embedded server left on the classpath, and auto-configuration, which configures whatever server it finds on the classpath, picks it up with no other code changes. The same pattern swaps in any other supported server.

So when a server swap seems to do nothing, check the dependency tree for the old server: a starter that brings in more than one piece of functionality also decides which implementation comes with it.