Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Wednesday, April 15, 2020

Spring Boot Remove Embedded Tomcat Server, Enable 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.

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

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

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

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


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.

No comments:

Post a Comment

Please do not add any spam links in the comments section.