Configure embedded Jetty server in spring boot
Spring boot use tomcat as default embedded tomcat server to run a web application but you can configure jetty server to replace tomcat in easy steps.
Add spring-boot-starter-jetty dependency
We need to do two things here:-
- Exclude default dependency
spring-boot-starter-tomcat
added inspring-boot-start-web
- Add
spring-boot-starter-jetty
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>
That’s it. You have replaced tomcat with jetty server.
Observations
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.jar
► tomcat-embed-el-x.y.z.jar
► tomcat-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
When you start spring boot application, You will in the logs that jetty is serving your web application now:-
2020-04-25 13:25:02.208 - INFO [main] c.a.s.SpringbootApplication : Starting SpringbootMicroserviceApplication on Ashishs-MBP with PID 2910
2020-04-25 13:25:02.213 - INFO [main] c.a.s.SpringbootApplication : No active profile set, falling back to default profiles: default
2020-04-25 13:25:03.317 - INFO [main] o.s.b.w.e.j.JettyServletWebServerFactory : Server initialized with port: 8080
2020-04-25 13:25:03.320 - INFO [main] org.eclipse.jetty.server.Server : jetty-9.4.27.v20200227; built: 2020-02-27T18:37:21.340Z; git: a304fd9f351f337e7c0e2a7c28878dd536149c6c; jvm 1.8.0_211-b12
2020-04-25 13:25:03.350 - INFO [main] o.e.j.s.h.ContextHandler.application : Initializing Spring embedded WebApplicationContext
2020-04-25 13:25:03.476 - INFO [main] o.e.jetty.server.handler.ContextHandler : Started o.s.b.w.e.j.JettyEmbeddedWebAppContext@30c3ae63
2020-04-25 13:25:03.476 - INFO [main] org.eclipse.jetty.server.Server : Started @2273ms