By default, Spring Boot uses Tomcat as an embedded server. This post describes how to replace Tomcat with the Jetty server using Maven and Gradle configuration.
When you use Spring Web starter it ships with an embedded tomcat server. To replace the tomcat server with jetty first we need to exclude it from the Maven pom or Gradle build file.
It is only a Two-Step process.
The application is tested with the below tools and technologies,
Spring Boot 2.6.0-RC1
JDK 8 (1.8)
Eclipse IDE
The default tomcat version in Spring Boot 2.6.0-RC1 is Apache Tomcat/9.0.54
Maven
Maven Step 1: Exclude Tomcat from Spring Web starter dependency
The Spring Web starter will look like this,
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Now we have to exclude the tomcat from spring-boot-starter-web,
<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>
Maven Step 2: Add Jetty Dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
Now Jetty is set as the embedded server. After adding Jetty server starter spring-boot-starter-jetty run the spring boot application as Spring Boot App. The console log will be like this,
Jetty also runs on 8080 port by default. The Jetty server version is 9.4.29. There is no change in port number and we didn't notice any difference in the server starting time. Starting times of both servers are almost equal.
Gradle
Gradle (Buildship 3.x) Type is chosen in Spring Starter Project while adding a new Spring Boot Project.
Default build.gradle file will look like,
plugins {
id 'org.springframework.boot' version '2.6.0-RC1'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.digitizedpost.springboot.gradle.helloworld'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/milestone' }
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
After excluding tomcat and including the jetty server, Right-click the project then do Gradle -> Refresh Gradle Project. Gradle Refresh will remove the tomcat and pull jetty server dependencies.
Gradle Step 1:
Under the dependencies section, in implementation configuration exclude Tomcat from Spring Web starter.
Gradle Step 2:
Include a starter in implementation http://localhost:8080/ for Jetty Server.
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'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
For testing the changes we have added a sample controller. It will run on http://localhost:8080/. On hitting the URL it will return the String.
Sample Rest Controller
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
@RequestMapping("/")
public String hello() {
return "Hello World from Spring Boot Demo App";
}
}