Step 2
Step 2 — First Spring Boot 4 project
30 min
Step 2 — First Spring Boot 4 project
start.spring.io is the standard starting point.
Settings
- Project: Gradle - Kotlin DSL
- Language: Java
- Spring Boot: 4.0.x
- Java: 21
- Dependencies:
Spring Web,Spring Data JPA,PostgreSQL Driver,Lombok,Validation
Generate → unzip → open in IntelliJ or VS Code.
First controller
@RestController
@RequestMapping("/api/hello")
public class HelloController {
@GetMapping
public String hello() { return "Hello, Spring!"; }
@GetMapping("/{name}")
public String greet(@PathVariable String name) { return "Hello, " + name + "!"; }
}
@RestController = JSON-response controller. @GetMapping = GET only.
Run
./gradlew bootRun
Default port 8080. Visit http://localhost:8080/api/hello.
Folder structure
demo/
├── build.gradle.kts
├── src/main/
│ ├── java/com/example/demo/
│ │ ├── DemoApplication.java ← entry (@SpringBootApplication)
│ │ └── HelloController.java
│ └── resources/
│ ├── application.yml ← DB URL, port
│ └── static/
└── src/test/
Try it
Add @PostMapping("/echo") that echoes the request body. Use @RequestBody Map<String, Object> for auto JSON parsing.
Next
Step 3 treats SQL as the single source of truth.