Spring as a Java container abstraction – switching to Undertow

Spring MVC abstracts the Java servlet container implementation away from you almost completely, this allows you to migrate from Tomcat quite easily.

This post is part of the “Spring Boot Primer” series.

Unless you have somehow tied yourself into Tomcat, perhaps by customising its configuration, or directly taking advantage of some Tomcat-specific feature, then all that is required is taking the default spring-boot-starter-web dependency:

pom.xml
1
2
3
4
5
6
7
8
<dependencies>
  ...
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  ...
</dependencies>

and then replacing it with the following:

pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<dependencies>
  ...
  <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-undertow</artifactId>
  </dependency>
  ...
</dependencies>

All this does is exclude Tomcat from the web starter, and lets you add either Undertow or Jetty as an alternative Java web container.

Enjoying this blog series? Take a look at our other technical blog posts