Enterprise Java

Spring Boot Remove Embedded Tomcat Server, Enable Jetty Server

A quick guide to exclude embedded tomcat server in spring boot application and adding Jetty Server Instead. Configuration to remove tomcat and add Jetty Server.

1. Introduction

In this tutorial, We’ll learn how to remove the Tomcat server from the Spring Boot application. Actually, Spring boot by default comes up with the embedded server once we add “spring-boot-starter-web” dependency.

But, Spring boot gives us the flexibility to use tomcat or not. If we do not want we can exclude this default server.

Default, Spring boot comes with 3 types of embed servers Tomcat, Jetty and undertow.

First, we’ll see how to exclude tomcat and next add jetty server.

Create the First Spring Boot Application and How to Test Rest API.

2. Tomcat By Default

Once we add spring-boot-starter-web dependency as part of pom.xml for web application development with spring boot, it gets the tomcat along with all required dependencies. It is always convenient to use directly and auto deployable to tomcat.

1
2
3
4
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

But, there are some scenarios, you do not need to use tomcat as part of Spring Boot application when using JMS instead of a web app or want to add Jetty.

3. Exclude Tomcat – Maven Pom.xml 

To exclude tomcat from spring boot, just need to add an additional block to the Spring Boot Starter dependency. In the dependency section, We can add
<exclusions> tags that make sure the given artifact is removed at build time.

This is the easiest way to do it.

01
02
03
04
05
06
07
08
09
10
<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>

You can use that approach to exclude Tomcat from Spring Boot and also for any other exclusions

4. Exclude Tomcat and All Servers – Annotation

When declaring the @SpringBootApplication annotation, there is a way to exclude all servers and do consider the spring boot application like the web.

To make spring boot as a non-web application, use the following.

1
@SpringBootApplication(exclude = {EmbeddedServletContainerAutoConfiguration.class, WebMvcAutoConfiguration.class})

And need to add the below property to non-rest applications so that spring boot does not try to start the
WebApplicationContext. This should go to the application.properties.

1
spring.main.web-environment=false

5. Add Jetty Server in Spring Boot

If you want to use the Jetty server in Spring boot application, first you must need to disable the default tomcat server and then add jetty dependency “
spring-boot-starter-jetty“.

1
2
3
4
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

After adding jetty in pom.xml then at build time it disables tomcat and maps to the Jetty configurations.

6. Gradle – Exclude tomcat and Add Jetty

This is quite easy then maven. Just add the tomcat at the exclusions section and add jetty in the dependencies section.

1
2
3
4
5
6
7
8
configurations {
    compile.exclude module: "spring-boot-starter-tomcat"
}
  
dependencies {
    compile("org.springframework.boot:spring-boot-starter-web:2.0.0.BUILD-SNAPSHOT")
    compile("org.springframework.boot:spring-boot-starter-jetty:2.0.0.BUILD-SNAPSHOT")
}

7. Conclusion

In this article, We’ve seen how to disable tomcat via pom.xml and annotation level. If you annotation level then it completely disables the web application feature. It is always recommended to use maven exclusion.

And also seen how to add the Jetty server.

Published on Java Code Geeks with permission by Venkatesh Nukala, partner at our JCG program. See the original article here: Spring Boot Remove Embedded Tomcat Server, Enable Jetty Server

Opinions expressed by Java Code Geeks contributors are their own.

Venkatesh Nukala

Venkatesh Nukala is a Software Engineer working for Online Payments Industry Leading company. In my free time, I would love to spend time with family and write articles on technical blogs. More on JavaProgramTo.com
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
sitapati
4 years ago

u mean to say tomcat removed from Springboot

Indranil Sharma
Indranil Sharma
11 months ago

After exclusion of Tomcat and use Jetty in POM level .I am facing error.

org.springframework.context.ApplicationContextException: Unable to start web server
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:164) ~[spring-boot-3.0.6.jar:3.0.6]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:578) ~[spring-context-6.0.8.jar:6.0.8]

Back to top button