Saturday 27 July 2019

Changing the embedded tomcat server in spring boot application?

How to Change the Embedded tomcat server with other embedded Server (Jetty or Undertow) in spring boot application?

I got this question many times and want to provide answer here:

To change the tomcat server with Undertow, replace the following dependencies:

1. Remove the spring-boot-starter-web dependency from pom.xml
   
     <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
     </dependency>

2. Add the following dependencies

Here we will exclude the spring-boot-starter-tomcat dependency 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>
      <dependency>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-undertow</artifactId>
      </dependency>

Now Run the application and check the console output, you will see: Undertow started on port(s) 8080: (In my case, it is default port, you can also change the port in application.properties file).

Screenshot:


Undertow Screenshot




To change the tomcat server with Jetty, replace the following dependencies:

1. Remove the spring-boot-starter-web dependency from pom.xml
   
     <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
     </dependency>

2. Add the following dependencies

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

Now Run the application and check the console output, you will see: Jetty started on port(s) 8089 (Port which you have set for server, default port 8080).

Jetty output: