Spring Boot Embedded Jetty Server Configuration

Learn to enable and configure a Spring Boot application to run on the embedded Jetty server in this step-by-step guide.

1. Default Behavior

By default, Spring Boot uses the embedded Tomcat server to run the Spring WebMVC application. Embedded Tomcat is transitively included with spring-boot-starter-web dependency.

Similarly, for reactive/webflux applications, Reactor Netty is the default embedded server.

At times, we may need to use the Jetty server in place of the Tomcat/Netty server. For example, Jetty is known for its lightweight nature and small footprint. Thus, Jetty is ideal for resource-constrained environments or microservices architectures where minimizing memory and CPU usage is crucial.

2. Configuring Embedded Jetty Server

Spring Boot bundles Tomcat and Jetty dependencies as separate starters to help make this process as easy as possible. We can use the jetty by following simple steps:

Step 1. Add spring-boot-starter-jetty and Exclude spring-boot-starter-tomcat

For Spring web MVC applications, We will need to update pom.xml and add the dependency for spring-boot-starter-jetty. Also, you will need to exclude the default added spring-boot-starter-tomcat 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>

In Gradle, this change can be achieved by this:

configurations {
    compile.exclude module: "spring-boot-starter-tomcat"
}
 
dependencies {
    compile("org.springframework.boot:spring-boot-starter-web:2.0.0.BUILD-SNAPSHOT")
    compile("org.springframework.boot:spring-boot-starter-jetty:2.0.0.BUILD-SNAPSHOT")
}

For the reactive webflux applications, exclude the spring-boot-starter-reactor-netty dependency from spring-boot-starter-webflux, and include Jetty.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-reactor-netty</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

Step 2. Jetty Configuration

2.1. Properties Configuration

To override the default runtime configuration, we can configure the Jetty properties in application.properties file.

server.port=8080
server.servlet.context-path=/home

####Jetty specific properties########

server.jetty.acceptors= # Number of acceptor threads to use.
server.jetty.max-http-post-size=0 # Maximum size in bytes of the HTTP post or put content.
server.jetty.selectors= # Number of selector threads to use.

2.2. Java Configuration

Prior to Spring Boot 2, we had to use the ConfigurableEmbeddedServletContainer or its specific subclasses to configure embedded servers. For example, JettyEmbeddedServletContainerFactory was used to configure embedded Jetty.

@Component
public class CustomContainer implements EmbeddedServletContainerCustomizer {
 
    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {
        if (container instanceof JettyEmbeddedServletContainerFactory) {

            JettyEmbeddedServletContainerFactory container = 
              (JettyEmbeddedServletContainerFactory) container;

            container.setPort(8080);
            container.setContextPath("/myapp");
        }
    }
}

Since Spring Boot 2, the EmbeddedServletContainerCustomizer interface is replaced by WebServerFactoryCustomizer, while the ConfigurableEmbeddedServletContainer class is replaced with ConfigurableServletWebServerFactory. It is the recommended approach for Spring Boot 3 as well.

import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.stereotype.Component;

@Component
public class JettyCustomizer implements WebServerFactoryCustomizer<JettyServletWebServerFactory> {

    @Override
    public void customize(JettyServletWebServerFactory factory) {

        factory.setPort(8080);
        factory.setContextPath("/myapp");
        // Add any additional Jetty configurations if needed
    }
}

3. Conclusion

This tutorial discussed the steps to exclude the default Tomcat server and include Jetty as an embedded server in a Spring boot application.

Drop me your questions in the comments section.

Happy Learning !!

Ref: Configure Jetty

Comments

Subscribe
Notify of
guest
6 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.