Introduction
Before we start coding, we need to prepare the development environment properly. A good setup will save you hours of debugging and significantly increase your productivity. This article will guide installation from A-Z and create the first Spring Boot 4.x project.
1. Install JDK 21+
1.1 Why JDK 21?
Spring Boot 4.x requires Java 17 or later, but JDK 21 (LTS) or JDK 25 is recommended because:
- Virtual Threads (Project Loom): Available from Java 21
- Pattern Matching: Switch expressions, record patterns
- Sealed Classes: Type-safe hierarchies
- Long-term Support: Supported until 2029+
1.2 Installation on OSes
macOS (with Homebrew + SDKMAN):
# Cài đặt SDKMAN
curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"
# Cài đặt JDK 21
sdk install java 21.0.4-tem
# Verify
java -version
# openjdk version "21.0.4" 2024-07-16 LTS
Ubuntu/Debian:
sudo apt update
sudo apt install -y openjdk-21-jdk
# Hoặc dùng SDKMAN (khuyến nghị)
sdk install java 21.0.4-tem
Windows:
# Dùng winget
winget install EclipseAdoptium.Temurin.21.JDK
# Hoặc download từ https://adoptium.net/
1.3 Managing multiple Java versions
SDKMAN allows easy switching between versions:
# Liệt kê các phiên bản có sẵn
sdk list java
# Cài thêm JDK 25
sdk install java 25.0.1-tem
# Switch version
sdk use java 21.0.4-tem # Cho session hiện tại
sdk default java 21.0.4-tem # Set mặc định
2. Install Build Tool
2.1 Maven vs Gradle
| Criteria | Maven | Gradle |
|---|---|---|
| Config format | XML (pom.xml) | Groovy/Kotlin (build.gradle) |
| Performance | Slower | Faster (incremental build) |
| Learning curve | Easier | Higher |
| Spring Boot support | Good | Good |
| Market share | ~60% | ~40% |
In this series, we will use Maven for the main part and have Gradle examples in the advanced lessons.
2.2 Install Maven
# macOS
brew install maven
# hoặc
sdk install maven
# Ubuntu
sudo apt install maven
# Verify
mvn -version
# Apache Maven 3.9.x
Note: Spring Boot uses Maven Wrapper (
mvnw), you are not required to install Maven globally. The project will automatically download the appropriate Maven version.
3. Install IDE
3.1 IntelliJ IDEA (Recommended)
IntelliJ IDEA is the best IDE for Java/Spring Boot development:
- Community Edition: Free, enough for Spring Boot
- Ultimate Edition: Has Spring Boot specific tools, database tools
# macOS
brew install --cask intellij-idea-ce
# Hoặc download từ https://www.jetbrains.com/idea/
Required plugins:
- Spring Boot (available in Ultimate)
- Lombok
- .env files support
3.2 VS Code
If you prefer VS Code, you need to install Extension Pack:
Extension Pack for Java (Microsoft)
Spring Boot Extension Pack (VMware/Broadcom)
Extensions include:
- Language Support for Java
- Debugger for Java
- Spring Boot Tools
- Spring Initializr Java Support
3.3 Install additional tools
# Docker (cho database, Redis...)
brew install --cask docker
# HTTPie hoặc curl cho test API
brew install httpie
# PostgreSQL client
brew install postgresql@16
# jq cho parse JSON
brew install jq
4. Initialize Project with Spring Initializr
4.1 Using the Web Interface
Access start.spring.io and configuration:
Project: Maven
Language: Java
Spring Boot: 4.0.5
Group: com.example
Artifact: demo
Name: demo
Packaging: Jar
Java: 21
Dependencies:
✅ Spring Web
✅ Spring Data JPA
✅ PostgreSQL Driver
✅ Spring Boot DevTools
✅ Lombok
✅ Validation
4.2 Using Command Line
# Dùng Spring CLI
curl https://start.spring.io/starter.tgz \
-d type=maven-project \
-d language=java \
-d bootVersion=4.0.5 \
-d groupId=com.example \
-d artifactId=spring-boot-demo \
-d name=spring-boot-demo \
-d packageName=com.example.demo \
-d javaVersion=21 \
-d dependencies=web,data-jpa,postgresql,devtools,lombok,validation \
| tar -xzvf -
4.3 Using IntelliJ IDEA
File → New → Project → Spring Boot
→ Server URL: https://start.spring.io
→ Chọn dependencies tương tự
→ Create
5. Project structure
5.1 Directory Layout
spring-boot-demo/
├── mvnw # Maven Wrapper (Linux/Mac)
├── mvnw.cmd # Maven Wrapper (Windows)
├── pom.xml # Maven build file
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ └── com/example/demo/
│ │ │ └── DemoApplication.java # Entry point
│ │ └── resources/
│ │ ├── application.properties # Configuration
│ │ ├── static/ # Static files
│ │ └── templates/ # Template files
│ └── test/
│ └── java/
│ └── com/example/demo/
│ └── DemoApplicationTests.java # Test class
└── .gitignore
5.2 pom.xml — Build Configuration
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>4.0.5</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>spring-boot-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-demo</name>
<description>Spring Boot 4 Demo Project</description>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
5.3 Main Application Class
package com.example.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);
}
}
@SpringBootApplication is a meta-annotation that combines 3 annotations:
@SpringBootConfiguration: Mark class as configuration source@EnableAutoConfiguration: Turn on auto-configuration@ComponentScan: Scan components in the current package and sub-packages
6. Run the application first
6.1 Create a simple Controller
package com.example.demo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
import java.util.Map;
@RestController
@RequestMapping("/api")
public class HelloController {
@GetMapping("/hello")
public Map<String, Object> hello() {
return Map.of(
"message", "Xin chào từ Spring Boot 4!",
"timestamp", LocalDateTime.now(),
"java", Runtime.version().toString()
);
}
@GetMapping("/health")
public Map<String, String> health() {
return Map.of("status", "UP");
}
}
6.2 Configuring application.properties
# Server
server.port=8080
# Application
spring.application.name=spring-boot-demo
# Tạm thời disable JPA (chưa có database)
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
6.3 Build and run
# Cách 1: Maven Wrapper
./mvnw spring-boot:run
# Cách 2: Build JAR rồi chạy
./mvnw clean package -DskipTests
java -jar target/spring-boot-demo-0.0.1-SNAPSHOT.jar
# Cách 3: Từ IDE
# Click Run button trên class DemoApplication
6.4 Test API
# Dùng curl
curl http://localhost:8080/api/hello | jq
# Dùng HTTPie
http GET localhost:8080/api/hello
# Output:
# {
# "message": "Xin chào từ Spring Boot 4!",
# "timestamp": "2026-03-30T10:30:00",
# "java": "21.0.4+7-LTS"
# }
7. Spring Boot DevTools
7.1 Automatic Restart
DevTools automatically restarts the application when code changes:
# application.properties
spring.devtools.restart.enabled=true
spring.devtools.restart.poll-interval=2s
spring.devtools.restart.quiet-period=1s
7.2 LiveReload
DevTools also supports LiveReload for the browser (automatically refreshes the website when there are changes).
Note: Spring Boot 4.1 has deprecated LiveReload support. It is recommended to use frontend build tools instead.
7.3 Development vs Production
DevTools is automatically disabled when running from a (production) JAR file. No need to worry about DevTools affecting production.
Summary
- Install JDK 21+ (recommended to use SDKMAN to manage versions), Maven, and IDE (IntelliJ IDEA or VS Code)
- Spring Initializr (start.spring.io) helps to quickly initialize projects with necessary dependencies
- Project Spring Boot has a clear structure: src/main/java for code, src/main/resources for configuration, src/test for tests
Exercises
- Install JDK 21 and Maven on your machine. Run
java -versionandmvn -versionto confirm - Create a new Spring Boot project with Spring Initializr, add dependencies
Spring Web, run and accesshttp://localhost:8080 - Try changing the server port to 9090 by editing
application.properties. Create 2 new endpoints:/api/infoReturns application information and/api/timereturns the current time