Introduction

Spring Boot web starter uses Tomcat as the default embedded servlet container or Tomcat is the default embedded server. So you don’t need to add any separate dependency for this Tomcat server. The Tomcat server is shipped with the spring boot starter web dependency.

Now let’s say your requirement is to not use the default server Tomcat but you would like to use Jetty. Therefore in this example we will see how to use Jetty instead of Tomcat in Spring Boot application.

Configurations

Generally we need to exclude the default embedded Tomcat server that comes with Spring Boot framework and use the Jetty starter in the dependency either in maven pom.xml file or gradle build.gradle file.

Maven based Configuration

If you use maven based Spring Boot application then do the Tomcat exclusion from your web starter as shown below:

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

Then add the Jetty started dependency as shown below:

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

Gradle based Configuration

If you use gradle based Spring Boot application then do the Tomcat exclusion from your web starter in build.gradle script as shown below:

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

Then add the Jetty started dependency as shown below:

compile("org.springframework.boot:spring-boot-starter-jetty")

So we have seen how to use Jetty instead of Tomcat in Spring Boot application. Now your application will run on Jetty server. You can also change the port of the server in application.properties file.

Thanks for reading.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *