Updated March 28, 2023
Introduction to Spring Boot Starter Web
Spring Boot Starter is one of the modules in the genre of Spring Boot which allows Java developers for the development of stand-alone and production-grade spring applications which is intended to “just run”. One can just get started with minimum configurations without thinking of setting up of Spring configuration. The main intent with which Spring Boot was conceptualized is to avoid writing of complex XML configurations in Spring and in turn reduce the development time. Spring enables users to run applications developed independently. Now, talking about the Spring Boot starter web, this particular module is specifically designed for writing rest endpoints. In the next few sections, we will take a deep dive into the Spring starter Web version of Spring Boot.
What is Spring Boot Web?
For any of Spring projects, one of the essential portions is the pom.xml file. POM or Project Object Module is the fundamental unit of Maven. It is basically an XML file that contains information about projects and its related configuration details. The conceptualization of Spring Boot Web is to accomplish three main goals which are:
- Reduction of manually added dependencies and bind all the required dependencies into one.
- A quick standardized structure of production-ready applications building.
- Structuring the version control within the XML itself so that the application is free from version errors.
Using the below XML structure, we import the required dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Here when we declare spring-boot-starter-web dependency, all imports required in this starter pack are internally imported and then added to your project. If one looks at the dependencies inside this starter pack, one would see some dependencies are direct whereas others are referred to other templates wherein those dependencies are transitively downloaded. Though version control is automatically taken care of in the pom.xml, in case the developer needs to have a particular version, it is readily available at the comfort of xml tag <version><\version>.
Spring Boot web is one of the many starter packs present with Spring Boot. The starter packs help in convenient collection of dependencies descriptors that would be essential for the application running.
Features of Spring Boot Web
Now that we have a fair understanding of what Spring Boot Starter Web is let us look at two main features of Web starter and then maybe deep dive into some essential discussions for this topic.
The features are:
- Spring Boot starter web is compatible with web development with common and essential dependencies included in the declaration itself.
- Spring Boot Web also has a feature for auto-configuration.
The feature discussed above helps in translating web development to such an easier level that one just needs to focus on the logic of the application without even thinking about setting the infrastructure or even do away with the fear of losing some essential imports.
Also, one needs to keep in mind that we do have tomcat server embedded with the project imports and one can make changes as and when required. The spring web uses tools like Spring MVC, REST, Tomcat as default embedded servers and helps the developer to reduce the build dependency count. In the below list we would go through some starter packs on which the web starter transitively depends:
- springframework.boot: spring-boot-starter-validation
- springframework.boot: spring-boot-starter
- springframework: spring-webmvc
- springframework.boot: spring-boot-starter-tomcat
- fasterxml.jackson.core: jackson-databind
- springframework.boot: spring-boot-starter-validation
- springframework: spring-web
Examples of Spring Boot Starter Web
Now that we have a fair idea on the features Spring Boot web provides and the various genre it caters to, let us look at one example to get an overall feeling of how the system works with Spring Boot web. To start off we would need to add the POM requirements which will be something like:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Now let us say that we won’t use the tomcat server and in turn use the Jetty server in our application. In that case, we would need to add the following:
<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>
In this, the exclusion tag means that the particulars noted in exclusions will not be included and manually we would need to add in the <dependency> tag about the jetty server we would use. Once we have incorporated all the essentials in POM, we are good to go with the application development.
Below we would create a web controller for a simple web application.
package com.example.project;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
public class EduCBACtrl {
@RequestMapping("/home")
public String index() {
return "Welcome to the tutorial from EduCBA!";
}
}
In our project, the application is under src/main/java/com/example/project/EduCBACtrl.java path. We would need to import RestController and RequestMapping from the pre-defined classes in the respective libraries. RestController is a convenient annotation, which is useful for the creation of RESTful services using Spring MVC. Here the RestController takes care of the mapping of request data to a defined handler method. Next, the RequestMapping is defined for routing of incoming HTTP requests to the corresponding handler method. One can use the RequestMapping at a class level or even at a method level. In our example, the /home is where the method underneath it is mapped to.
This is how it would look like at the end this application is run:
Advantages of Spring Boot Web
Now that we have a fair understanding of how things work with Spring Boot web, let us look at the advantages Spring Boot brings in so that one can develop applications on Spring Boot with confidence.
- Spring Boot web applications can be built easily with Java or Groovy.
- A lot of boilerplate coding is avoided once the developer uses Spring Boot to build their applications.
- As a result of the reduction of redundant work, productivity increases and saves a lot of development time.
- The concept of plug and play is pretty much evident with the plugins provided by Spring Boot.
Conclusion
At last, to conclude, Spring Boot does offer a lot of features for the easy development of applications. One can think about this as a robot which would do the task of repetitive work of taking care of the infrastructure and its internal dependencies which would enable smooth development and running of the application.
Recommended Articles
This is a guide to Spring Boot Starter Web. Here we discuss the Introduction and features of spring boot starter web along with advantages and examples. you may also have a look at the following articles to learn more –