Build Your First Spring Boot Application
Welcome to the foundational part of learning Spring Boot! In this guide, you’ll learn the essential concepts needed to start building robust Java applications with Spring Boot. We’ll cover what Spring Boot is, how to set up your development environment, create your first project, and understand its structure. You’ll also learn about dependency management, how to create a basic web controller, and how to run your application.
Prerequisites
Before you begin, ensure you have the following:
- A solid understanding of Java, including object-oriented programming concepts (classes, methods, interfaces).
- Familiarity with relational databases and SQL (tables, primary keys, foreign keys, basic queries).
Understanding Spring Boot
Spring Boot is a powerful framework built on top of the Spring framework. While the Spring framework provides a comprehensive toolbox for Java applications, it often requires significant configuration. Spring Boot simplifies this by offering sensible defaults and pre-configured features, allowing you to get applications up and running in minutes rather than hours. It reduces boilerplate code and lets you focus on building features.
The Spring ecosystem includes various projects like Spring Data for database access, Spring Security for authentication, and Spring Cloud for microservices. Spring Boot acts as an entry point, making it easier to leverage these components.
Setting Up Your Development Environment
To get started, you’ll need a few tools:
Install the Java Development Kit (JDK)
Download and install the latest version of the JDK from Oracle’s website (or your preferred distribution). Verify the installation by opening your terminal or command prompt and running
java -version.Choose a Code Editor
IntelliJ IDEA is highly recommended for its intelligent code completion and Spring Boot integration. You can try the Ultimate Edition free for 3 months. Alternatives include VS Code and Eclipse.
Install a Build Automation Tool (Maven)
Spring Boot uses build tools like Maven or Gradle to manage dependencies and build projects. This course uses Maven. IntelliJ IDEA comes with Maven built-in, so you don’t need a separate installation if you’re using it. If not, you can download Maven from Maven Apache or install it using package managers like Homebrew (macOS) or Chocolatey (Windows). Verify the installation with
mvn -v.
Creating Your First Spring Boot Project
You can create a Spring Boot project in two ways:
Using start.spring.io
Visit the website, select your build tool (Maven), language (Java), Spring Boot version (latest stable), and project metadata (group, artifact ID). Choose your Java version and click ‘Generate’. Unpack the downloaded zip file.
Using IntelliJ IDEA (Recommended)
Open IntelliJ IDEA, click ‘New Project’, select ‘Spring Initializr’ (available in Ultimate Edition), configure your project name, location, language, build tool, group ID, artifact ID, and JDK version. You can also optionally create a Git repository. Click ‘Create’.
Understanding the Project Structure
A typical Spring Boot project structure includes:
.idea/: IntelliJ IDEA configuration files.mvnw/mvnw.cmd: Maven Wrapper scripts to ensure consistent builds..gitignore: Specifies untracked files for Git.help.md: Getting started instructions.pom.xml: The Project Object Model file for Maven, defining project configuration and dependencies.src/main/java: Contains your application’s Java source code.src/main/resources: Contains non-Java files like configuration (application.properties) and static assets.src/test/java: Contains your automated test code.
The entry point for your application is typically a class annotated with @SpringBootApplication, containing a main method that calls SpringApplication.run().
Dependency Management
Spring Boot uses starter dependencies, which are curated collections of libraries designed to work together. Instead of manually adding individual libraries, you add a starter dependency (e.g., spring-boot-starter-web), and it brings in all the necessary libraries (like an embedded web server, JSON processing, and web framework components).
To add a dependency:
- Open your
pom.xmlfile. - You can manually add the dependency coordinates from Maven Central.
- Alternatively, use IntelliJ IDEA’s dependency management feature: press
Ctrl+N(Windows) orCmd+N(Mac), select ‘Dependency’, search for the starter (e.g.,spring-boot-starter-web), and add it. - After adding a dependency, click the ‘Reload All Maven Projects’ icon (usually a cylinder with arrows) in IntelliJ IDEA to download and apply the changes.
Best Practice: Remove the version number from your dependency declarations in pom.xml. Spring Boot’s parent project (spring-boot-starter-parent) manages the versions of these dependencies, ensuring compatibility and simplifying upgrades.
Creating Your First Web Controller
Spring MVC follows the Model-View-Controller pattern. In Spring Boot, you can create a controller to handle incoming web requests.
Create a Controller Class
In your
src/main/javadirectory, create a new Java class (e.g.,HomeController) within your package.Annotate the Class
Add the
@Controllerannotation above the class definition. This tells Spring that this class handles web requests.Define a Request Mapping
Inside the controller class, create a method (e.g.,
index()). Annotate it with@RequestMapping("/")to map it to the root URL of your application. This method should return the name of the view to be rendered.Example:
@Controller public class HomeController { @RequestMapping("/") public String index() { return "index"; // Refers to index.html } }Create the View
In the
src/main/resources/directory, create astaticfolder if it doesn’t exist. Inside thestaticfolder, create an HTML file namedindex.html. Add your desired HTML content, for example:<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Home Page</title> </head> <body> <h1>Hello World!</h1> </body> </html>
Running Your Application
To run your Spring Boot application:
- Using IntelliJ IDEA: Click the green play icon next to your main application class or on the toolbar (
Ctrl+RorCmd+R). - Using Maven: Open your terminal in the project’s root directory and run
./mvnw spring-boot:run(ormvnw.cmd spring-boot:runon Windows).
Once the application starts, you’ll see output in the console indicating that an embedded web server (like Tomcat) has started, usually on port 8080. Open your web browser and navigate to http://localhost:8080 to see your application running.
Source Code Access
You can access the source code for each lesson on GitHub at github.com/mhamedani/spring-boot-course. Each commit represents a lesson, and tags are available for the beginning of each section.
Source: Spring Boot Tutorial for Beginners [2025] (YouTube)