🎯 What you will learn
REST maps CRUD operations to standard HTTP verbs. Spring Boot makes this mapping explicit and type-safe. By the end of this lesson you will:
- Map GET, POST, PUT, DELETE to Java methods using shortcut annotations
- Extract path segments with
@PathVariableand query strings with@RequestParam - Deserialise a JSON request body with
@RequestBody - Control the HTTP status code and headers independently using
ResponseEntity - Return 200, 201, 204, and 404 correctly for different operations
- Set a common URL prefix for a controller with
@RequestMapping
🌍 Real-world scenario
Samuel needs four endpoints — list, get-one, create, delete — for the transfers resource. Each maps cleanly to a standard HTTP verb, returns JSON, and uses @PathVariable / @RequestBody to bind data.
🗺️ Block 1 — Full CRUD controller
All four HTTP verbs live in one controller class. @RequestMapping at class level sets the base path; individual method annotations append to it.
ResponseEntity explained: ResponseEntity.ok(body) → 200, ResponseEntity.notFound().build() → 404, ResponseEntity.status(201).body(x) → 201. It decouples HTTP semantics from your business logic.
🔍 Block 2 — GET by ID + 404 handling
@PathVariable binds a URI template variable to a method parameter. When the resource is not found, returning ResponseEntity.notFound().build() sends a proper HTTP 404 instead of throwing an exception.
✉️ Block 3 — POST create transfer
@RequestBody tells Jackson to deserialise the HTTP request body JSON into a Java object. The response should be 201 Created with the persisted resource in the body — not 200 OK.
Content-Type matters: Without -H "Content-Type: application/json" Spring Boot will reject the body with 415 Unsupported Media Type. Always set it explicitly when sending a JSON body.
📋 HTTP verb → Spring annotation mapping
| HTTP Verb | Spring Annotation | Typical Status | Use case |
|---|---|---|---|
| GET | @GetMapping | 200 OK | Read one or many |
| POST | @PostMapping | 201 Created | Create new resource |
| PUT | @PutMapping | 200 OK | Replace entire resource |
| PATCH | @PatchMapping | 200 OK | Partial update |
| DELETE | @DeleteMapping | 204 No Content | Remove resource |
🎤 Interview Q&A
@PathVariable extracts from the URL path template (/transfers/{id}), while @RequestParam reads query-string parameters (/transfers?status=PENDING). Use @PathVariable for resource identity and @RequestParam for filtering, sorting, or pagination.
ResponseEntity gives you control over the HTTP status code and headers. Returning the object directly always sends 200 OK, even if you intended 201 or 404. For RESTful APIs following HTTP semantics correctly, ResponseEntity is the right choice.
Return ResponseEntity and call .header("X-Trace-Id", traceId) on the builder, or inject HttpServletResponse and call response.setHeader(). Example: ResponseEntity.ok().header("X-Request-Id", uuid).body(data).
PUT replaces the entire resource — send all fields. PATCH partially updates — send only changed fields. Spring Boot supports both via @PutMapping / @PatchMapping. For partial updates in Java, use a DTO with nullable fields or Optional wrappers to distinguish "not sent" from "set to null".
The DispatcherServlet consults a list of HandlerMappings (RequestMappingHandlerMapping being the most common). It matches the request URL, HTTP method, and consumes/produces content-type to find the best HandlerMethod. Then HandlerAdapters convert arguments (@PathVariable, @RequestBody etc.) before invoking the method. The return value is processed by HttpMessageConverter (Jackson for JSON) and written to the response.
📝 Quiz — 5 questions
Next: Dependency Injection and Beans →
⚠️ Common mistakes
/transfers/{id} requires @PathVariable Long id, not @RequestParam. @RequestParam is for ?id=42 query-string parameters.ResponseEntity.status(201) for POST creation; returning the object directly gives 200 OK, which is semantically incorrect per REST conventions.-H "Content-Type: application/json" Spring Boot rejects the body with 415 Unsupported Media Type. Always set it when sending a JSON body.AmbiguousHandlerMappingException at startup. Each combination of path + HTTP verb must be unique within a controller.✅ Key takeaways
- @GetMapping / @PostMapping / @PutMapping / @DeleteMapping map HTTP verbs
- @PathVariable extracts URI segments; @RequestBody deserialises JSON
- ResponseEntity controls status code, headers, and body independently
- 200 OK for reads, 201 Created for POST, 204 No Content for DELETE
- @RequestMapping at class level sets a common prefix for all routes
From routes to a full payments API
Join the Best AI Programs — three 5-month courses, fully online, with placement assistance. Week 1 is free.
Explore our AI-era courses →