When building a Spring Boot application with Maven, one of the core dependencies you need to include is the spring-boot-starter-web. This starter package provides all the necessary libraries to build web applications, including support for RESTful web services. In this guide, we will show you how to include the Spring Boot Web dependency in your Maven pom.xml
file.
Step-by-Step Guide to Adding the Spring Boot Web Dependency in Maven
1. Edit your pom.xml File
To include the Spring Boot Web dependency, you need to add the following dependency entry into the <dependencies>
section of your Maven pom.xml
file:
xml
<dependencies>
<!-- Other dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
2. Complete Example of pom.xml
Here is a complete example of a pom.xml
file that includes the Spring Boot Web dependency:
xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>my-spring-boot-app</artifactId>
<version>1.0.0</version>
<properties>
<java.version>11</java.version>
<spring-boot.version>2.5.2</spring-boot.version>
</properties>
<dependencies>
<!-- Other dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Maven plugins -->
</plugins>
</build>
</project>
3. Explanation of the pom.xml
Configuration
- GroupId and ArtifactId: Replace
com.example
with your desired group name and my-spring-boot-app
with the name of your project. - Spring Boot Version: The version of Spring Boot you use can be specified in the
<properties>
section. In this example, 2.5.2
is used. Make sure to adjust it to the version that fits your project requirements. - Java Version: This configuration specifies that the project uses Java 11. You can modify this based on your environment.
- Spring Boot Starter Web: The
<dependency>
for spring-boot-starter-web
is the core part. It includes dependencies for Spring MVC, embedded Tomcat server, and other essential components for creating web applications.
4. Building and Running Your Application
After adding the dependency to your pom.xml
, you can proceed to build your Spring Boot application by running Maven commands. Maven will automatically download all the necessary dependencies, including the Spring Web libraries, for you.
To build and run the application, execute the following Maven commands in your terminal:
bash
mvn clean install
bash
mvn spring-boot:run
Your Spring Boot application should now be up and running with the necessary web components provided by the spring-boot-starter-web
dependency.
Conclusion
By adding the spring-boot-starter-web
dependency to your Maven pom.xml
, you've equipped your Spring Boot project with everything needed to develop a web-based application. This includes support for creating RESTful APIs, integrating with front-end frameworks, and embedding a web server like Tomcat.