Spring Boot Starter Web

It has two important features mentioned below:

  • First, it is compatible with Web Development
  • Second, Automatic Configuration

 When developing a web application, you need to add the following dependencies to the pom.xml file:

<dependency>  
<groupId>org.springframework.boot</groupId>  
<artifactId>spring-boot-starter-web</artifactId>  
<version>2.2.2.RELEASE</version>  
</dependency>  

It uses Spring MVC, REST, and Tomcat as the default embedded server. Today, most applications require a Model-View-Controller (MVC) architecture to meet various requirements, including processing user data, providing application efficiency, and providing the dynamic nature of the application. It was primarily used to create graphical desktop user interfaces (GUIs) but is now becoming more popular in creating web-based applications. MVC is not a technology stack but an architectural pattern that provides three key logical components: model, view, and controller.

Model: A model is a data that operates starter web. It represents the data passed between the controller and other logic. The controller can retrieve data (models) from databases and users.

Views: Views are part of an application that displays model data. The data is from database format or user input. It is an output presentation from manipulated data such as tables, charts, and graphs to the user.

Controller: The controller handles the user's interaction with the application. Receives mouse and keyboard input from the user and modifies the model and view concerning the input.

The only spring-boot-starter-web dependency transitively takes out other dependencies associated with web development. It also brings down the count of build dependency. The spring-boot-starter-web transitively is connected with the following:

  • org.springframework.boot:spring-boot-starter
  • org.springframework.boot:spring-boot-starter-tomcat
  • org.springframework.boot:spring-boot-starter-validation
  • com.fasterxml.jackson.core:jackson-databind
  • org.springframework:spring-web
  • org.springframework:spring-webmvc

By default, spring-boot-starter-web contains the following Tomcat server dependencies:

<dependency>  
<groupId>org.springframework.boot</groupId>  
<artifactId>spring-boot-starter-tomcat</artifactId>  
<version>2.0.0.RELEASE</version>  
<scope>compile</scope>  
</dependency>  

Spring-boot-starter-web automatically configures the following for web development:

  • Dispatcher Servlet
  • Error page
  • WebJAR
  • Built-in Servlet container for managing static dependencies

Spring Boot Embedded Web Server

All Spring Boot applications come with an embedded server. The embedded server is embedded as part of the deployable application. The dominance of embedded servers is that you don't need a server installed before in your environment. If we discuss Spring Boot, it has the default embedded server known as Tomcat. It also supports the following embedded servers.

  • Jetty Server
  • Undertow Server

Few more embedded web server

When discussing applications that are based on servlet stack, it also contains Tomcat by using spring-boot-starter-tomcat. Still, several other options, too, like using spring-boot-starter-jetty or spring-boot-starter-undertow instead.

Now for reactive stack applications, the spring-boot-starter-web flux contains Reactor Netty, including spring-boot-starter-reactor-netty. In this case, also we can use other options, like spring-boot-starter-tomcat, spring-boot-starter-jetty, or spring-boot-starter-undertow instead.

Jetty Server

Another embedded server held up by the spring boot known as Jetty Server. It can be defined as HTTP server and Servlet container that can serve uniform and non-uniform content. Utilization of Jetty Server can be observed where machine-to-machine connection is required.

If you add a Jetty server to your application, you must add a spring-boot-starter-jetty dependency to your pom.xml file.

Note: If your application uses a jetty server, you need to take into consideration that the default Tomcat server is not contained in the spring-boot-starter web. Avoid conflicts between servers.

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

It can also be utilized to manipulate the behavior of the Jetty server application.properties file.

Undertow Server

Spring Boot provides another server called Undertow. It's also an embedded web server that is similar to Jetty. Undertow server is written in Java which is maintained and sponsored by JBoss. Following are the pros of the Undertow server: 

  • Holds up HTTP/2
  • HTTP upgrade supports
  • Holds up web-socket
  • Provides support for Servlet 4.0
  • Flexible

Note:  If your application uses an undertow server, ensure that the default Tomcat server is excluded from the Spring-boot-starter-web. Avoid conflicts between servers.

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

You have another feature to customize the Undertow server's behavior with the application's help.properties file.

Spring-boot-starter-web vs. spring-boot-starter-tomcat

Spring-boot-starter-web: It provides the Spring web dependencies that contains spring-boot-starter-tomcat. Following are some features of spring-boot-starter-web:

  • Containing, spring-boot-starter
  • Jackson
  • spring-core
  • spring-MVC
  • spring-boot-starter-tomcat

 Spring-boot-starter-tomcat: It has everything relevant to the Tomcat server.

  • core
  • el
  • logging
  • WebSocket

If we talk about starter-tomcat, it contains the following dependencies:

<dependency>  
<groupId>org.apache.tomcat.embed</groupId>  
<artifactId>tomcat-embed-core</artifactId>  
<version>8.5.23</version>  
 <scope>compile</scope>  
</dependency>  
<dependency>  
<groupId>org.apache.tomcat.embed</groupId>  
<artifactId>tomcat-embed-el</artifactId>  
<version>8.5.23</version>  
<scope>compile</scope>  
</dependency>  
<dependency>  
<groupId>org.apache.tomcat.embed</groupId>  
<artifactId>tomcat-embed-websocket</artifactId>  
<version>8.5.23</version>  
<scope>compile</scope>  
</dependency> 

You can use spring-MVC even if you don't have a built-in Tomcat server. If you do this, you must exclude the Tomcat server by using the <exclusion> tag, as shown in the code below.

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