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:

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


demo/
โ”œโ”€โ”€ mvnw
โ”œโ”€โ”€ pom.xml
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ main/
โ”‚   โ”‚   โ”œโ”€โ”€ java/
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ com/kscodes/springboot/demo
โ”‚   โ”‚   โ”‚       โ””โ”€โ”€ DemoApplication.java
โ”‚   โ”‚   โ””โ”€โ”€ resources/
โ”‚   โ”‚       โ”œโ”€โ”€ application.properties
โ”‚   โ””โ”€โ”€ test/

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

Here’s a simplified version of your Maven config:





	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		3.5.3
		 
	
	com.kscodes.springboot
	demo
	0.0.1-SNAPSHOT
	demo
	Demo project for Spring Boot
	
	
		
	
	
		
	
	
		
		
		
		
	
	
		21
	
	
		
			org.springframework.boot
			spring-boot-starter
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
  
      org.springframework.boot
      spring-boot-starter-web
    
  
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	



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

File: DemoApplication.java


package com.kscodes.springboot.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

}

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

File: HelloController.java


package com.kscodes.springboot.demo;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String sayHello() {
        return "Hello, World! 
" + "Welcome to KSCodes!
" + "This is our first message โ€” a simple one, but classic:"; } }

โ–ถ๏ธ 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