Spring Boot Starter Web
There are two important features of spring-boot-starter-web:
If we want to develop a web application, we need to add the following dependency in pom.xml file:
Starter of Spring web uses Spring MVC, REST and Tomcat as a default embedded server. The single spring-boot-starter-web dependency transitively pulls in all dependencies related to web development. It also reduces the build dependency count. The spring-boot-starter-web transitively depends on the following:
By default, the spring-boot-starter-web contains the following tomcat server dependency:
The spring-boot-starter-web auto-configures the following things that are required for the web development:
Spring Boot Embedded Web Server
Each Spring Boot application includes an embedded server. Embedded server is embedded as a part of deployable application. The advantage of embedded server is, we do not require pre-installed server in the environment. With Spring Boot, default embedded server is Tomcat. Spring Boot also supports another two embedded servers:
Using another embedded web server
For servlet stack applications, the spring-boot-starter-web includes Tomcat by including spring-boot-starter-tomcat, but we can use spring-boot-starter-jetty or spring-boot-starter-undertow instead.
For reactive stack applications, the spring-boot-starter-webflux includes Reactor Netty by including spring-boot-starter-reactor-netty, but we can use spring-boot-starter-tomcat, spring-boot-starter-jetty, or spring-boot-starter-undertow instead.
The Spring Boot also supports an embedded server called Jetty Server. It is an HTTP server and Servlet container that has the capability of serving static and dynamic content. It is used when machine to machine communication is required.
If we want to add the Jetty server in the application, we need to add the spring-boot-starter-jetty dependency in our pom.xml file.
Remember: While using Jetty server in the application, make sure that the default Tomcat server is excluded from the spring-boot-starter-web. It avoids the conflict between servers.
We can also customize the behavior of the Jetty server by using the application.properties file.
Spring Boot provides another server called Undertow. It is also an embedded web server like Jetty. It is written in Java and manage and sponsored by JBoss. The main advantages of Undertow server are:
Remember: While using Undertow server in the application, make sure that the default Tomcat server is excluded from the spring-boot-starter-web. It avoids the conflict between servers.
We can also customize the behavior of the Undertow server by using the application.properties file.
spring-boot-starter-web vs. spring-boot-starter-tomcat
The spring-boot-starter-web contains the spring web dependencies that includes spring-boot-starter-tomcat. The spring-boot-starter-web contains the following:
While the spring-boot-starter-tomcat contains everything related to Tomcat server.
The starter-tomcat has the following dependencies:
We can also use spring-mvc without using the embedded Tomcat server. If we want to do so, we need to exclude the Tomcat server by using the <exclusion> tag, as shown in the following code.