How can we disable the default server and enable the different one?

Spring Boot allows developers to replace the default embedded server with another supported server by excluding the existing server dependency and adding the dependency of the desired server. This flexibility enables applications to use Tomcat, Jetty, or Undertow based on performance, deployment, or architectural requirements.

Key Points: • Tomcat is the default embedded server in Spring Boot. • The default server can be disabled by excluding its dependency. • A new server can be enabled by adding its starter dependency. • No application code changes are required when switching servers. • Spring Boot automatically configures the newly added server.

Default Server in Spring Boot

When we add:

spring-boot-starter-web

Spring Boot automatically includes:

• Embedded Tomcat • Spring MVC • Jackson • Validation libraries

As a result, Tomcat starts automatically when the application launches.

How to Switch the Embedded Server?

The process involves two steps:

1. Exclude the current server. 2. Add the new server dependency.

Switching from Tomcat to Jetty

Step 1: Exclude Tomcat

Code Example:

<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

Code Example:

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

After startup:

Embedded Jetty

will be used instead of Tomcat.

Switching from Tomcat to Undertow

Step 1: Exclude Tomcat

Code Example:

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

Code Example:

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

Spring Boot will automatically configure Undertow during startup.

How It Works Internally

Application Starts | Classpath Scanned | Tomcat Excluded | Jetty/Undertow Detected | Auto-Configuration Applied | Selected Server Starts

Spring Boot chooses the available server implementation and configures it automatically.

Example: Suppose a project initially uses:

spring-boot-starter-web

Result:

Embedded Tomcat Starts

After excluding Tomcat and adding:

spring-boot-starter-jetty

Result:

Embedded Jetty Starts

The application code remains unchanged.

Why Change the Embedded Server?

Common reasons include:

• Performance optimization • Reduced memory consumption • Organization standards • Specific server features • Production environment requirements

Server Options

Tomcat

• Default choice • Mature and widely used • Excellent community support

Jetty

• Lightweight • Suitable for microservices

Undertow

• High performance • Non-blocking architecture • Low memory footprint

Benefits of Embedded Server Switching

• Simple dependency-based configuration • No code changes required • Easy experimentation • Flexible deployment options • Full Spring Boot support

Real-World Example

An organization standardizes all microservices on Undertow for lower memory usage.

Developers simply:

1. Exclude Tomcat. 2. Add Undertow dependency.

Spring Boot automatically switches servers without requiring changes to controllers, services, or business logic.

Interview Tip: A concise interview answer is:

"To disable the default embedded server in Spring Boot, exclude its dependency from the project configuration. Then add the dependency of the desired server, such as Jetty or Undertow. Spring Boot's auto-configuration mechanism detects the new server on the classpath and automatically configures it without requiring application code changes."