Card 02/ 06
All 6 cards
ExampleDifficulty: Intermediate1 min
Path, Query and Body — Three Ways In
A request carries data in three different places, and a controller method has a separate annotation for each.
@RestController
@RequestMapping("/rides")
class RideController {
@GetMapping("/{id}")
ResponseEntity<Ride> getRide(@PathVariable String id) { /* ... */ }
@GetMapping
ResponseEntity<?> list(@RequestParam(defaultValue = "all") String status) { /* ... */ }
@PostMapping
ResponseEntity<Ride> createRide(@RequestBody Ride ride) { /* ... */ }
}| Annotation | Reads from | Example |
|---|---|---|
@PathVariable | A segment of the URL itself | /rides/42 → id = "42" |
@RequestParam | The query string | /rides?status=active → status = "active" |
@RequestBody | The request body, deserialized by Jackson | A JSON object → a Ride |