What embedded servers are supported by Spring Boot?
Spring Boot applications typically include an embedded web server to make them self-contained and easy to deploy. By default, Spring Boot uses Tomcat, but it provides support for several other popular embedded servers, allowing developers to choose the most suitable option for their specific needs.
Main Embedded Servers Supported
Spring Boot auto-configures one of the following servers based on the dependencies present in your classpath. You can also explicitly include or exclude them via your build tool (e.g., Maven or Gradle).
Apache Tomcat
Tomcat is the default embedded server provided by Spring Boot. It's a robust, mature, and widely used Servlet container. Most Spring Boot web applications start with Tomcat out-of-the-box due to its inclusion in the spring-boot-starter-web dependency.
Eclipse Jetty
Jetty is another powerful and highly customizable Servlet container known for its lightweight footprint and excellent performance. It's often preferred in scenarios where resource consumption is a critical factor or for projects that require a more modular and embeddable server.
To use Jetty instead of Tomcat, you would typically exclude Tomcat from spring-boot-starter-web and then add the spring-boot-starter-jetty dependency.
Undertow
Undertow is a flexible, non-blocking web server developed by Red Hat and used in WildFly application server. It's known for its high performance and its ability to handle a large number of concurrent connections efficiently. It supports both blocking and non-blocking APIs.
Similar to Jetty, to use Undertow, you would exclude Tomcat and include the spring-boot-starter-undertow dependency.
How to Switch Embedded Servers
You can easily switch between these servers by modifying your pom.xml (for Maven) or build.gradle (for Gradle) file. The general approach involves excluding the default Tomcat dependency from spring-boot-starter-web and then explicitly adding the starter for your desired server.
Example (Maven for Undertow)
<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>
This modular approach allows developers to choose the optimal server runtime for their Spring Boot applications based on performance requirements, resource constraints, and specific architectural choices.