Configure embedded Jetty server in Spring Boot Configure embedded Jetty server in Spring Boot

Page content

In this quick tutorial, we’ll configure embedded Jetty server by replacing it with default Tomcat server in Spring Boot web application.

Add Jetty Dependency

We need to do two things here:-

  1. Exclude default dependency spring-boot-starter-tomcat added in spring-boot-start-web
  2. Add spring-boot-starter-jetty dependency.
pom.xml
<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>

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

That’s it. You have replaced tomcat with jetty server.

When you build spring boot maven project, it smartly replace tomcat jars with jetty dependency jars from maven dependency as follows:

▼ Maven Dependencies
  ► tomcat-embed-core-x.y.z.jartomcat-embed-el-x.y.z.jartomcat-embed-websocket-x.y.z.jar
  ► jetty.servlets-x.y.z.jar
  ► jetty.continuation-x.y.z.jar
  ► jetty-http-x.y.z.jar
  ► jetty-webapp-x.y.z.jar
  ► jetty-servlet-x.y.z.jar
  ► jetty-security-x.y.z.jar
  ► jetty-server-x.y.z.jar

Application Startup Logs

When you start spring boot application, You will in the logs that jetty is serving your web application now:-

INFO c.e.demo.SpringBootDemoApplication       :  Starting SpringBootDemoApplication using Java 11.0.10 on Ashishs-MBP with PID 4221 (/Users/ashl/IdeaProjects/springboot-examples/springboot-config/build/classes/java/main started by ashl in /Users/ashl/IdeaProjects/springboot-examples/springboot-config)
DEBUG c.e.demo.SpringBootDemoApplication      :  Running with Spring Boot v2.5.0, Spring v5.3.7
INFO c.e.demo.SpringBootDemoApplication       :  No active profile set, falling back to default profiles: default
INFO org.eclipse.jetty.util.log               :  Logging initialized @1343ms to org.eclipse.jetty.util.log.Slf4jLog
INFO o.s.b.w.e.j.JettyServletWebServerFactory :  Server initialized with port: 8080
INFO org.eclipse.jetty.server.Server          :  jetty-9.4.41.v20210516; built: 2021-05-16T23:56:28.993Z; git: 98607f93c7833e7dc59489b13f3cb0a114fb9f4c; jvm 11.0.10+8-LTS-162
INFO o.e.j.s.h.ContextHandler.application     :  Initializing Spring embedded WebApplicationContext
INFO w.s.c.ServletWebServerApplicationContext :  Root WebApplicationContext: initialization completed in 708 ms
INFO o.e.jetty.server.handler.ContextHandler  :  Started o.s.b.w.e.j.JettyEmbeddedWebAppContext@49469ffa{application,/,[file:///private/var/folders/6p/3ztts6bd4xbdl7nkcq6lpcg80000gn/T/jetty-docbase.8080.2111859430883013110/, jar:file:/Users/ashl/.gradle/caches/modules-2/files-2.1/org.webjars/swagger-ui/3.49.0/e126393ccbab7950fb618b7662fb37f71cb20841/swagger-ui-3.49.0.jar!/META-INF/resources],AVAILABLE}
INFO org.eclipse.jetty.server.Server          :  Started @1597ms
INFO o.e.j.s.h.ContextHandler.application     :  Initializing Spring DispatcherServlet 'dispatcherServlet'
INFO o.s.web.servlet.DispatcherServlet        :  Initializing Servlet 'dispatcherServlet'
INFO o.s.web.servlet.DispatcherServlet        :  Completed initialization in 1 ms
INFO o.e.jetty.server.AbstractConnector       :  Started ServerConnector@712cfb63{HTTP/1.1, (http/1.1)}{0.0.0.0:8080}
INFO o.s.b.web.embedded.jetty.JettyWebServer  :  Jetty started on port(s) 8080 (http/1.1) with context path '/'
INFO c.e.demo.SpringBootDemoApplication       :  Started SpringBootDemoApplication in 1.813 seconds (JVM running for 2.131)

Add Spring Boot Jetty Configuration

Spring boot also provides Jetty server specific configuration which you can configure from application.yml or application.properties file.

application.yml
server:
  jetty:
    connection-idle-timeout: # Time that the connection can be idle before it is closed.
    max-http-form-post-size: # Maximum size of the form content in any HTTP post request e.g. 200000B
    accesslog:
      enabled: # Enable access log e.g. true
      append: # Enable append to log e.g. true
      custom-format: # Custom log format
      file-date-format: # Date format to place in log file name
      filename: # Log file name, if not specified, logs redirect to "System.err"
      format: # Log format e.g ncsa
      ignore-paths: # Request paths that should not be logged
      retention-period: # Number of days before rotated log files are deleted e.g. 31
    threads:
      acceptors: # Number of acceptor threads to use. When the value is -1, the default, the number of acceptors is derived from the operating environment.
      selectors: # Number of selector threads to use. When the value is -1, the default, the number of selectors is derived from the operating environment.
      min: # Minimum number of threads e.g. 8 
      max: # Maximum number of threads e.g. 200
      max-queue-capacity: # Maximum capacity of the thread pool's backing queue. A default is computed based on the threading configuration.
      idle-timeout: # Maximum thread idle time in millisecond e.g. 60000ms      

You can refer to the Spring Boot official documentation for full list of configuration.