In this tutorial, we will learn how to change the default web server in a Spring Boot application. By default, Spring Boot uses an embedded Tomcat server to run our application. However, there might be scenarios where we want to use a different web server, such as Jetty or Undertow.
Step 1: Add the necessary dependencies
To switch to a different web server in Spring Boot, we need to include the corresponding dependency in our project’s build configuration file (e.g., pom.xml for Maven). For example, if we want to use Jetty as our web server, we need to add the following dependency:
org.springframework.boot spring-boot-starter-jetty
Step 2: Exclude the default web server dependency
Since Spring Boot includes the default Tomcat dependency by default, we need to exclude it from our project when using a different web server. We can achieve this by adding the exclusion configuration in our build configuration file. For example:
org.boot spring-boot-starter-web org.boot spring-boot-starter-tomcat
Step 3: Configure the new web server properties (if required)
If the new web server requires additional configuration properties, we can specify them in our application.properties or application.yaml file. For instance, if we are using Jetty as our web server, we can configure the server port by adding the following line to our application.properties file:
server.port=8080
Step 4: Run the Spring Boot application
After completing the above steps, we are ready to run our Spring Boot application with the new web server. Once we start the application, it will use the specified web server instead of the default Tomcat server.
Conclusion
In this tutorial, we learned how to change the default web server in a Spring Boot application. By adding the necessary dependencies, excluding the default Tomcat dependency, and configuring any required properties, we can easily switch to a different web server such as Jetty or Undertow. This flexibility allows us to choose a web server that best suits our application’s requirements.