Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 52

Building a Spring Boot Project with Web and Validation Dependencies

Recommended resources

Welcome to Module 9! In our previous lesson, we concluded our exploration of the core Spring IoC container by examining bean scopes, specifically singleton and prototype. You now have a solid foundation in how Spring manages objects within an application.

Today, we transition from foundational concepts to practical application. Your goal of mastering enterprise backend development begins in earnest as we set up our first web project. This lesson will guide you through creating a Spring Boot project using the standard tool, Spring Initializr. We will focus on including the necessary dependencies for building web applications (Spring Web) and ensuring data integrity (Validation), which are the building blocks for the REST APIs we will construct in the subsequent lessons.

1. Bootstrapping Your Application with Spring Initializr

Every major software ecosystem has a tool for bootstrapping new projects—for example, create-react-app in the React world. For Spring, that tool is the Spring Initializr. It is a web-based service (also integrated into most IDEs) that generates a complete, ready-to-run Spring Boot project structure. It saves you from the tedious and error-prone process of manually setting up directories, build files, and core dependencies.

The Initializr lets you define key aspects of your project:

  • Build Tool: You can choose between Maven and Gradle. We will continue using Maven, so you'll be working with the pom.xml file to manage dependencies.
  • Language: We will, of course, be using Java.
  • Project Metadata: This includes the Group (typically your organization's reversed domain, like com.example), the Artifact (your project's name), and the derived Package Name.
  • Dependencies: This is where you select the "starters" and libraries your project will need. A "starter" is a convenient dependency descriptor that bundles a set of related libraries.

For this lesson, we will focus on two essential dependencies for building REST APIs:

  1. Spring Web: This starter includes everything needed for building web applications, including RESTful APIs. It bundles Spring MVC, an embedded Apache Tomcat server (so you don't need to deploy your app to an external server), and other necessary libraries.
  2. Validation: This starter includes the Jakarta Bean Validation APIs and an implementation (Hibernate Validator). It allows you to declaratively define and enforce validation rules on your data objects.

Let's walk through the process of creating our first project.


2. Creating Your First Spring Boot Project

The most direct way to start is by using the web interface at start.spring.io. The following article provides a concise, step-by-step guide. We'll follow its instructions to create a project skeleton that we will build upon in the coming lessons.

The Spring Initializr web interface, where you can configure and generate a new Spring Boot project. This is the starting point for almost all new Spring applications.

A guide to input validation with Spring Boot

This guide from Snyk will walk you through creating a project with the exact configuration we need. While the article's focus is on validation, its initial project setup steps are a perfect demonstration.

Please read the section titled Create a new project. Follow the instructions provided, but with one small change: for the Spring Boot version, select the latest stable version available (i.e., one that doesn't say SNAPSHOT or M1). The other settings—Maven, Java 17, and the Web and Validation dependencies—are exactly what we need. After configuring, click Generate to download the project zip file.

Once you have downloaded and extracted the zip file, you will have a complete project directory. Don't import it into your IDE just yet. First, let's understand what was just created for you.


3. Understanding the Project Structure

The Spring Initializr doesn't just create empty folders; it generates a standard, conventional project structure that is immediately recognizable to other Spring developers. Having a standard layout makes projects easier to navigate and maintain.

The standard directory layout for a Spring Boot application, as seen in an IDE. Key areas include `src/main/java` for source code and `pom.xml` for build configuration.

The article from OneUptime provides an excellent breakdown of this structure.

How to Set Up a Spring Boot Project from Scratch - OneUptime

This article explains the purpose of each file and directory generated by the Initializr.

First, read the section Understanding the Project Structure. This will familiarize you with the roles of the src/main/java and src/main/resources directories. Next, read the following section, The Main Application Class. Pay close attention to the explanation of the @SpringBootApplication annotation, as it is the magic that enables Spring Boot's auto-configuration.

The two most important files in your new project are:

  • pom.xml: As we've discussed, this is the heart of your Maven project. If you open it, you will see the Group and Artifact IDs you defined. More importantly, under the <dependencies> section, you will find entries for spring-boot-starter-web and spring-boot-starter-validation. The Initializr did the work of finding the correct versions and adding them for you.
  • ...Application.java: Inside src/main/java and your package structure, you'll find a main application class. This class contains the main method that serves as the entry point for your application and is annotated with @SpringBootApplication.

Now, let's watch a video that puts all these pieces together. This will reinforce the concepts and show you how the project is handled within IntelliJ IDEA.

Spring Boot Tutorial for Beginners [2025]

Programming with Mosh provides an excellent, detailed walkthrough of the entire project creation process, including a deep dive into how dependencies are managed.

Start by watching the segment on creating a project both on the website and within IntelliJ IDEA. This mirrors the steps you just took. The next segment, from dependency management, is crucial. It explains how "starter" dependencies work and how the spring-boot-starter-parent in your pom.xml manages the versions of all other dependencies to ensure they are compatible. This is a powerful concept that makes Spring Boot so robust.

At this point, you should feel comfortable importing the generated project into your IDE. IntelliJ IDEA will automatically recognize it as a Maven project, read the pom.xml, and download all the necessary dependencies.


Conclusion

In this lesson, you've taken a significant step from theory into practice. You have learned how to use Spring Initializr to bootstrap a fully configured Spring Boot application, setting the stage for all our future backend development work.

Key Takeaways:

  • Spring Initializr is the standard tool for creating Spring Boot projects.
  • A project is defined by its build tool (Maven), metadata (group, artifact), and dependencies.
  • Starters, like spring-boot-starter-web, simplify dependency management by bundling common libraries.
  • The generated project follows a standard structure, with pom.xml for build configuration and src/main/java for your code.
  • The @SpringBootApplication annotation is the entry point that enables Spring Boot's auto-configuration and component scanning features.

You now have an empty but functional web application project. In our next lesson, we will bring it to life by defining our first REST endpoints for CRUD operations using @RestController and mapping annotations.