Let’s walk through how to create a simple Spring Boot program — step by step. We’ll build a small “Hello World” REST API using Spring Boot.
ð§° Prerequisites
Make sure you have:
-
Java 17+ installed (
java -version
) -
Maven or Gradle
-
Spring Boot (via Spring Initializr)
-
An IDE (like IntelliJ IDEA, Eclipse, or VS Code)
ðŠ Step 1: Create a New Project
Option 1 — Use Spring Initializr (recommended)
Go to ð https://start.spring.io
Select:
-
Project: Maven
-
Language: Java
-
Spring Boot Version: Latest stable (e.g. 3.3.x)
-
Group:
com.example
-
Artifact:
demo
-
Dependencies:
-
✅ Spring Web
-
Click Generate, and it will download a .zip
file.
Extract and open it in your IDE.
ð§ą Step 2: Check Project Structure
Your project should look like:
demo/
├── src/
│ ├── main/
│ │ ├── java/com/example/demo/DemoApplication.java
│ │ └── resources/application.properties
│ └── test/
├── pom.xml
ð§ðŧ Step 3: Write a Simple REST Controller
Create a new file:
ð src/main/java/com/example/demo/HelloController.java
package com.example.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, Spring Boot!";
}
}
This creates a REST endpoint at /hello
.
ð Step 4: Run the Application
In your IDE, run DemoApplication.java
, or in the terminal:
mvn spring-boot:run
You’ll see:
Tomcat started on port 8080
Now open your browser and go to:
ð http://localhost:8080/hello
You should see:
Hello, Spring Boot!
ð You’ve just built your first Spring Boot app!
⚙️ Step 5: Optional — Customize Port
In src/main/resources/application.properties
:
server.port=9090
Now your app runs on http://localhost:9090/hello.
ð§Š Step 6: Build JAR (optional)
To package it:
mvn clean package
Run it:
java -jar target/demo-0.0.1-SNAPSHOT.jar
Would you like me to show you how to connect this app to a database (like MySQL) or create CRUD APIs next?