Setting up Spring Boot with Maven (Step-by-Step Guide)

๐Ÿงฐ What Youโ€™ll Learn

In this guide, weโ€™ll walk through how to:

  • Generate a Spring Boot project using Spring Initializr
  • Set up and understand the Maven pom.xml
  • Write your first simple REST API
  • Run the project and test it

๐Ÿ› ๏ธ Prerequisites

Before you start, make sure you have:

  • Java 17 or 21 installed (java -version)
  • Maven installed (mvn -v)
  • An IDE like IntelliJ IDEA, Eclipse, or VS Code

โšก Step 1: Generate a Spring Boot Project

Go to Spring Initializr:

  • Project: Maven
  • Language: Java
  • Spring Boot: 3.5.x (latest stable)
  • Group: com.kscodes.springboot
  • Artifact: demo
  • Name: demo
  • Description: Demo project for Spring Boot with Maven
  • Packaging: Jar
  • Java Version: 17 or 21
  • Dependencies:
    • Spring Web

Click Generate, and a .zip file will be downloaded. Extract and open it in your IDE.

Spring Boot with Maven

๐Ÿ“ฆ Step 2: Project Structure Overview

๐Ÿงพ Step 3: Maven pom.xml Essentials

Here’s a simplified version of your Maven config:

๐Ÿ’ก Step 4: Your First Spring Boot Application

File: DemoApplication.java

๐Ÿ” Step 5: Add a Simple REST Controller

File: HelloController.java

โ–ถ๏ธ Step 6: Run the Application

Run the app using any of the following methods:

Method 1: From IDE

Run DemoApplication.java as a Java application.

Method 2: From Command Line

mvnw spring-boot:run

๐ŸŒ Step 7: Test Your REST Endpoint

Open your browser or use a tool like Postman:

Setting up Spring Boot with Maven

๐ŸŽ‰ Summary

Youโ€™ve just created your first Spring Boot 3.x application using Maven. You:

  • Generated a project with Spring Initializr
  • Understood the Maven structure
  • Built and ran a basic REST API