Spring Boot Application in Embedded Jetty Server

This quick tutorial demonstrates How to Run Spring Boot Application in Embedded Jetty Server.

Embedded Jetty Server

Spring Boot applications contain embedded servers and when we run them, they run inside the embedded servers. Tomcat is the default embedded server in Spring Boot. However, we may want to run our application in an embedded Jetty server.

Spring Boot provides starter packages for both of these servers. As Tomcat is the default server, the Web starter dependency implicitly contains tomcat starter package. Next, we will learn how to use Maven and Gradle to enable Jetty Server in Spring Boot application.

Enable Jetty Server Using Maven

As started earlier, the web starter package implicitly adds tomcat starter as its dependency. In order to disable Tomcat, we need to exclude tomcat starter from the web starter 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>Code language: HTML, XML (xml)

Next, step is to add the spring-boot-starter-jetty package. And, we have done everything required to run Spring boot application in Jetty.

Enable Jetty Server Using Gradle

If we are using gradle and not maven be can do the same thing in build.gradle file.

implementation('org.springframework.boot:spring-boot-starter-web'){
    exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
}

compile("org.springframework.boot:spring-boot-starter-jetty")Code language: Gradle (gradle)

Jetty Basic Configurations

Spring Boot provides all necessary default configurations for Jetty server, so that it is ready to use. However, we may wish to change some of the default configurations. To do that we can use application properties or yaml file.

Configure Jetty Http Post Request Size

We can set maximum size of a POST request body using

server.jetty.max-http-form-post-size=200000BCode language: Properties (properties)

Configure Jetty Request Idle Timeout

server.jetty.connection-idle-timeout=10sCode language: Properties (properties)

Configure Jetty Maximum and Minimum Number of Threads

server.jetty.threads.max=200
server.jetty.threads.min=8Code language: Properties (properties)

Configure Jetty Maximum Request Header Size

server.max-http-header-size=8KBCode language: Properties (properties)

Enable Jetty Server Access Logs

server.jetty.accesslog.enabled=trueCode language: Properties (properties)

Summary

In this quick tutorial, we have seen that we can easily Run a Spring Boot Application in Jetty Server. Spring boot provides starter dependencies for both Tomcat and Jetty Server. In order to use jetty server, we need to add the jetty starter package dependency and also we need to exclude the tomcat starter dependency from the web starter. We have also covered some of the Basic configurations for the Embedded Jetty Server.