Spring Boot Starter Web: Bootstrap a Spring Boot Web Application

Spring Boot Starter Web

The Spring Boot starters are one of the essential modules for bootstrapping a Spring Boot application with minimal configurations. The Spring Boot Starter Web is one of the Spring Boot Starters for developing the web-based Spring Boot applications.

The Spring Boot Starters easily import the necessary configuration for the Spring Boot project. Thus, we can bootstrap a web application easily with minimum configuration using the spring-boot-starter-web.

Spring Boot Starters are one of the major reasons why developers are switching their technology to Spring Boot from other technologies. It reduces the developer’s effort and avoid writing lots of boilerplate configuration codes.

Spring Boot starter web is specifically designed for writing rest endpoints.

Let’s discuss what is exactly starter web with its features.

In this section, we have included:

What is Spring Boot Starter Web

The spring-boot-starter-web is a Spring Boot starter used to bootstrap a web-based Spring Boot application. It automatically initialize a spring boot project and import all the related configuration itself.

In other words, a starter web configuration provides a production-ready Spring Boot web application.

The main motive behind using it is to accomplish the following three goals:

  • Initializing a project which is compatible with web development.
  • Reducing the effort which is made on adding dependencies manually and bind all the required dependencies into one.
  • Providing auto-configuration to our project
  • Providing a standardized structure to our application.
  • Providing easy dependency management
  • Structuring and managing the version control within the XML itself so that the application is free from version errors.

We can easily add a starter web to our project by putting the below configuration code in our pom.xml file.

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

Once we have declare the starter web to our project, it will import all the required configuration jar files to our project.

Spring boot starter web is one of the Spring Starters packs provided by Spring Boot. It helps in the convenient collection of dependencies descriptors necessary for the project.

Spring Boot web starter uses Spring MVC, Rest, and Tomcat as default embedded server. The single spring-boot-starter-web pulls all the dependencies required for web development. Furthermore, It reduces the build dependency count.

The spring-boot-starter-web transitivity dependes on 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 it contains the following dependency for the embedded tomcat server:

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

The spring-boot-starter-web auto-configures several required components for the web development, which are listed as follows:

  • Dispatcher Servlet
  • Error Page
  • Web JARs for managing the static dependencies
  • Embedded servlet container

See spring-boot-starter-web Maven repository.

Spring Boot Embedded Server

by default, each Spring Boot application contains an embedded server, which is a part of the deployable application. The advantage of having an embedded server is that we do not require a pre-installed server in the environment. The default embedded server in Spring Boot is Tomcat, but, we can specify the following other two servers:

  • Jetty Server
  • Undertow Server

How to use another embedded web server

As we have discussed by default the Spring Boot contains a Tomcat server but we can specify Jetty and Undertow servers as well. The starter web contains spring-boot-starter-tomcat as the default server, to change it we can replace it by defining the spring-boot-starter-jetty or spring-boot-starter-undertow.

For reactive stack applications, the spring-boot-starter-webflux includes Reactor Netty by including spring-boot-starter-reactor-netty.

Using Jetty Server as Default Spring Boot Server

The Spring Boot also supports another embedded server like Tomcat called Jetty Server. It is an HTTP server and Servlet container that is capable enough for serving the static and dynamic content. It useful when machine-to-machine communication is performed.

To add the Jetty server in our Spring Boot application, we will have to add the spring-boot-starter-jetty dependency in our pom.xml file.

Always remember to exclude the embedded Tomcat server from the spring-boot-starter-web, before adding the Jetty server. Otherwise, there may be conflicts between the 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>  

To customize the behavior of the Jetty server, update them in the application.properties file.

Using Undertow Server as Default Spring Boot Server

Just like Tomcat and Jetty, spring Boot supports another server called Undertow. It is also an embedded server in Spring Boot. It is sponsored by JBoss and written in Java. Some key advantages of using the Undertow server are as following:

  • Supports HTTP/2
  • HTTP upgrade support
  • Websocket Support
  • Provides support for Servlet 4.0
  • Flexible
  • Embeddable

Remember: While using the 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.

Always remember to exclude the embedded Tomcat server from the spring-boot-starter-web, before adding the Undertow server. Otherwise, there may be conflicts between the 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>  

To customize the behavior of the Undertow server, update them in the application.properties file.

Difference between spring-boot-starter-web and spring-boot-starter-tomcat

one question may raise in your mind; is there any serious comparison between them? The answer is No, but, we can distinguish them in other aspects. We are adding this discussion in this article because some users get confused between them. This discussion will clear the picture.

The spring-boot-starter-web holds the several web dependencies; the spring-boot-starter-tomcat is one of them. The starter parent contains the following major components:

  • spring-boot-starter
  • jackson
  • spring-core
  • spring-mvc
  • spring-boot-starter-tomcat

Whereas, the spring-boot-starter-tomcat contains components that are related to the Tomcat server. It holds the following components:

  • core
  • el
  • logging
  • websocket

The spring-boot-starter-tomcat 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> 

Can We Use the Spring MVC without Using the embedded Tomcat Server?

Now, after the discussing SB starter web and embedded servers, you may have a question that can we use the Spring MVC without using the embedded server. The answer is Yes; we can use the spring-mvc without using the embedded server. To do so, we must exclude the tomcat server by using the <exclusion> tag. Consider the below code:

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

From the above code, we have excluded the embedded Tomcat server from our project configuration. Now, we can run the project using the external servers.

Advantages of Spring Boot Starter Web

Now, we have discussed almost every aspect of the SB starter web, and have a clear understanding of how things work in it. Let’s discuss some of its advantages:

  • The web application can be developed easily.
  • Minimum configuration is required.
  • Avoid writing lots of boilerplate code.
  • Increase productivity and saves time.
  • Provides plug-ins to deal with the dependencies
  • Easy maintenance.

Conclusion

Adding a final note to this discussion, we would like to say that Spring Boot provides lots of features and starters for easy development. Using SB starter web, we can easily develop and manage the web-based Spring Boot applications.

See Also,

Spring Boot Starters

Spring Boot Starter Parent

Spring Boot Dependencies

Spring Boot Annotations