SpringTechnology

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>

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

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=200000B

Configure Jetty Request Idle Timeout

server.jetty.connection-idle-timeout=10s

Configure Jetty Maximum and Minimum Number of Threads

server.jetty.threads.max=200
server.jetty.threads.min=8

Configure Jetty Maximum Request Header Size

server.max-http-header-size=8KB

Enable Jetty Server Access Logs

server.jetty.accesslog.enabled=true

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.