Card 02/ 06

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.

java
@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) { /* ... */ }
}
Where each one reads from
AnnotationReads fromExample
@PathVariableA segment of the URL itself/rides/42 → id = "42"
@RequestParamThe query string/rides?status=active → status = "active"
@RequestBodyThe request body, deserialized by JacksonA JSON object → a Ride