REST API CRUD in Practice — From Start to Finish
REST API CRUD in Practice — From Start to Finish
🎯 After Reading This Lesson
After reading this lesson, you will be confident doing all three of the following:
- ▸✅ Implement all four REST API operations (GET/POST/PUT/DELETE) using @RestController + @RequestMapping
- ▸✅ Understand and use the differences between @RequestBody, @PathVariable, and @RequestParam
- ▸✅ Apply a global exception handling pattern with @RestControllerAdvice
Keep these learning goals as a checklist, and close the lesson once you can answer all of them.
The Four HTTP Methods of CRUD
The Core Contract of REST
URLs are nouns (resources), actions are HTTP methods — that is the essence of REST.
1. User Domain + DTO
Separating Entity and DTO is the standard approach. DTOs are exclusively for API input/output, while Entities are exclusively for database mapping.
2. Temporary Storage (In-Memory Instead of a Database)
3. Service Layer
The Controller does not touch the Repository directly — the Service layer owns the business logic.
Controller — @RequestBody · @PathVariable · ResponseEntity
Roles of the Four Annotations
- ▸
@RequestBody— Deserializes the JSON in the HTTP body into an object - ▸
@PathVariable— Extracts values like{id}from the URL path - ▸
@RequestParam— Extracts query string values like?page=1 - ▸
ResponseEntity— Gives you direct control over status code + headers + body
Full Controller Code
When the Response Body Is an Object — Jackson Serializes Automatically
When @RestController is present, all return values are automatically converted to JSON. You can return a Map, List, or DTO directly without any extra configuration.
When You Need to Be Explicit About the Status Code
For GET requests, just return the object. For POST, return 201. For DELETE, return 204. Use ResponseEntity when you need to make those codes explicit.
In practice, some teams standardize all responses with ResponseEntity, while others return plain objects for simple GET calls. Both are valid approaches.
Global Exception Handling — @ControllerAdvice
try-catch Everywhere Is a Nightmare
try-catch in dozens of methods → boilerplate explosion.
@ControllerAdvice — A Dedicated Exception-Handling Bean
All exceptions from every controller are handled in one place. Controller code can then focus purely on business logic.
Custom Exception Classes
Extending RuntimeException means no throws declaration required. Creating domain-specific exceptions (UserNotFoundException, OrderNotFoundException) makes the intent of the code explicit.
curl Testing
Postman users can save all four requests as a Collection for one-click testing.
🤖 Try Asking AI Like This
Once you understand the concepts in this lesson, you can give AI specific, precise instructions. Instead of a vague 'fix this,' you can make vocabulary-driven requests — and that is where token savings begin.
- ▸'Add global exception handling based on @ControllerAdvice to this controller.'
- ▸'Create a controller with four User CRUD API endpoints (GET/POST/PUT/DELETE).'
- ▸'Wrap these responses in ResponseEntity and make sure 201 and 204 status codes are returned correctly.'
Why This Reduces Token Usage
Without understanding the concepts, even after receiving an AI response you end up asking 'What does that mean?' again. That follow-up question is what burns through tokens. Learn the concept once, and the conversation ends in a single exchange.