Introduction about Spring Boot Starter WebMVC

When building a web application with the Spring Framework, we often need many different components such as Spring Web, Spring MVC, JSON support, and a web server runtime like Apache Tomcat to run the application. Declaring each dependency individually and managing their versions would make creating a Spring web application quite time-consuming. Spring Boot solves this problem through its Starters.

One of the most commonly used Starters I want to mention in this article is the Spring Boot Starter WebMVC:

With just this dependency, we have most of the necessary components to build a web application or REST API using Spring MVC.

So what exactly is spring-boot-starter-webmvc? What dependencies does it provide? And why can we build a REST API almost immediately after adding this dependency? We’ll explore this in this tutorial.

Before exploring spring-boot-starter-webmvc, let’s briefly explain what a Starter is in Spring Boot.

Simply put, a Spring Boot Starter is a dependency descriptor that groups together frequently used dependencies for a specific purpose. This makes declaring dependencies much simpler. Additionally, Spring Boot provides a version control mechanism for its dependencies, ensuring that frequently used dependencies have compatible versions.

spring-boot-starter-webmvc is a Spring Boot Starter for building web applications using Spring MVC.

To use it, simply add the following dependency to your pom.xml file:

After adding this dependency, Maven will load spring-boot-starter-webmmvc along with the dependencies that this Starter requires.

In the example from the previous tutorial, you can see that many necessary dependencies are included in the project as follows:

Some important dependencies of the Spring Boot Starter WebMVC, which help us understand how this Starter works, are listed here:
  • Spring Boot Starter
  • Spring Web
  • Spring Web MVC and Spring Boot Web MVC
  • Spring Boot Tomcat and Spring Boot Starter Tomcat
  • Spring Boot Jackson and Spring Boot Starter Jackson

Now let’s look at the role of each dependency!

Spring Boot Starter

spring-boot-starter is the core library of Spring Boot Starters; every Starter uses it.

It helps us include the basic dependencies for a Spring Boot application as follows:

  • spring-boot
  • spring-boot-autoconfigure
  • spring-core
  • spring-context
  • logging support

Spring Web

spring-web is a module of the Spring Framework that provides fundamental web-related components. It offers many abstractions and infrastructures used in handling HTTP requests and responses. It relates to:

  • HTTP request and response
  • Web utilities
  • HTTP message conversion
  • REST-related infrastructure

Spring Web MVC and Spring Boot Web MVC

Another very important dependency is spring-webmvc. This is the module that provides Spring MVC.

Spring MVC provides a mechanism for applications to receive HTTP requests, find the appropriate controller, call methods to handle the request, and generate an HTTP response.

spring-boot-webmvc is the module that provides the auto-configuration and integration mechanism of Spring Boot. We will discuss this auto-configuration mechanism in a later article.

Embedded Tomcat

Another very noteworthy component is: spring-boot-starter-tomcat

This starter will bring embedded Tomcat into the application. This is one of the points that makes Spring Boot significantly different from the traditional way of deploying Java web applications.

Previously, to deploy applications to runtime servers, we would often package them as a WAR file and deploy them to a runtime server such as Apache Tomcat that was installed externally.

With Spring Boot, Tomcat is packaged with the application and can be started with the application.

Therefore, we can run the application using:

or after building it into a JAR:

We don’t need to install a separate Tomcat server just to run the application if we use the default embedded server.

This is one of the features that helps Spring Boot simplify the development and deployment of web applications.

JSON support

When building REST APIs, converting between Java objects and JSON is a very common need.

For example, we have a Java record:

When returning this object from a REST endpoint, the client may receive JSON:

This process can be visualized as follows:

  • Java Object -> JSON serialization -> JSON
  • In the reverse direction, JSON -> JSON deserialization -> Java Object

In Spring Boot, Jackson is a commonly used library for this conversion.

spring-boot-starter-webmvc integrates spring-boot-starter-jackson into the application, thereby providing the necessary dependencies for JSON support.

Therefore, when building a REST API with Spring Boot, we usually don’t need to write our own logic to convert Java objects to JSON or vice versa.

Auto configuration

This is where we can connect Starter to a very important concept of Spring Boot: Auto-configuration.

Spring Boot can rely on dependencies in the classpath and application environment to determine which configurations need to be applied. When the necessary dependencies for a web application are present, Spring Boot can automatically configure much of the necessary infrastructure for the application.

One of the auto-configuration classes related to Spring MVC is: WebMvcAutoConfiguration. Thanks to this mechanism, we don’t need to write a lot of configurations ourselves as we would with a pure Spring Framework. As I mentioned above, I will discuss this auto-configuration in detail in another article!

This is one of the core ideas of Spring Boot: Convention over Configuration

Instead of requiring developers to configure everything from scratch, Spring Boot provides reasonable configurations for common scenarios and allows developers to customize as needed.

For example, in the previous tutorial, we didn’t have to manually:

  • Install Tomcat;
  • Configure Servlet containers;
  • Create WAR files;
  • Configure Spring MVC infrastructure;
  • Write our own JSON converter.

Most of these tasks are handled by Spring Boot through Starter dependencies and auto-configuration mechanisms.

So, in this tutorial, I’ve introduced you to the basics of Spring Boot Starter Web MVC. Understanding its purpose will greatly help you in developing web applications with Spring Boot!

Add Comment