Learnitweb

How to Change the Embedded Server in Spring Boot

1. Exclude Tomcat

By default, spring-boot-starter-web brings in Tomcat. To use a different server, you must exclude Tomcat from it.

In Maven, this looks like:

<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>

2. Add the Alternative Server Dependency

Choose only one of the following:

a. Use Jetty

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

b. Use Undertow

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

c. Use Netty (only for reactive applications)

If you’re using spring-boot-starter-webflux, Netty is used by default.

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

⚠️ Don’t mix servlet-based (web) and reactive (webflux) starters in the same app.

3. Clean and Rebuild

Once you’ve excluded Tomcat and added your preferred server, do a clean build:

mvn clean install

This ensures all dependencies are updated correctly and Tomcat is fully removed.

4. Verify on Application Startup

When you run your Spring Boot app, check the logs:

  • For Jetty: Jetty started on port(s): 8080
  • For Undertow: Undertow started on port(s): 8080
  • For Netty (reactive): Netty started on port 8080

Full Maven Example: Switching from Tomcat to Jetty

<dependencies>
    <!-- Spring Web Starter without 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>

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

Other Configuration Options

Once you’ve switched the server, you can configure it in your application.properties or application.yml just like you would with Tomcat.

Examples:

server.port=9090
server.servlet.context-path=/api

Each server (Jetty, Undertow) may have custom tuning options, like thread pool size, buffer sizes, etc. Let me know if you’d like those settings.

When Should You Switch Servers?

ServerBest For
TomcatDefault, widely supported
JettyLightweight, embeddable in custom apps, async handling
UndertowHigh-performance, non-blocking I/O
NettyReactive applications with WebFlux (not Servlet-based)