In Week 2, we learned how to set up a professional Spring Boot project with Maven, YAML configs, and proper folder structure.
Now, in Week 3, it’s time to build REST APIs — the backbone of modern backend applications.
We’ll cover controllers, DTOs, validation, CRUD operations, and best practices for professional APIs.
What is a REST API?
REST (Representational State Transfer) is an architectural style for building web APIs.
A REST API lets clients (like web or mobile apps) communicate with your backend using HTTP methods.
| HTTP Method | Action | Example URL |
| ----------- | ------ | --------------- |
| GET | Read | /api/users |
| POST | Create | /api/users |
| PUT | Update | /api/users/{id} |
| DELETE | Delete | /api/users/{id} |
Step 1: Create a Simple REST Controller
In your project, create:
src/main/java/com/example/demo/controller/UserController.java@RestController
@RequestMapping("/api/users")
public class UserController {
@GetMapping
public String getAllUsers() {
return "List of all users";
}
@GetMapping("/{id}")
public String getUserById(@PathVariable Long id) {
return "User with ID: " + id;
}
}Key Annotations:
@RestController→ Combines@Controller + @ResponseBody@RequestMapping→ Base URL for all endpoints@GetMapping→ Handles HTTP GET requests@PathVariable→ Extracts path parameters
Step 2: Create a User Entity & DTO
User Entity
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
// Getters and Setters
}User DTO (Data Transfer Object)
public class UserDTO {
@NotBlank(message = "Name is required")
private String name;
@Email(message = "Invalid email")
private String email;
// Getters and Setters
}