Request Mapping & Parameters
Bind data from the request: @PathVariable for path segments, @RequestParam for query strings, @RequestBody for JSON payloads, and the right status codes for each operation.
Learn Request Mapping & Parameters in our free Java course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…
Part of the free Java course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
Finish Building REST Controllers first — this lesson builds on @RestController , @GetMapping / @PostMapping and ResponseEntity .
💡 Analogy: A request is like a delivery form. The address line (path) names exactly which house — that's your @PathVariable . The delivery options ticked at the bottom (query string) are your @RequestParam . The parcel itself (the body) is your @RequestBody . Spring reads the form and hands each part to the right method parameter.
Status codes are the courier's receipt: 201 "delivered & created", 404 "no such address", 204 "done, nothing to hand back".
A templated segment like /users/{" "} is captured with @PathVariable . Use this to identify a specific resource — the path is part of its identity.
@RequestParam binds query-string values like ?q=ann&page=2 . Add required=false or defaultValue to make a parameter optional.
For POST/PUT, @RequestBody deserialises the JSON body into a record or POJO via Jackson. Pair it with a 201 Created status for new resources.
Here is what each binding produces, simulated in plain Java so the path / query / body distinction is unmistakable.
Answer: @PathVariable for the path segment, @RequestParam for the query string.
Answer: @RequestBody deserialises it via a message converter (Jackson).
You can now bind every part of a request — path segments (@PathVariable), query strings (@RequestParam), JSON bodies (@RequestBody) and headers (@RequestHeader) — and return correct HTTP status codes for each operation.
Next up: The Service Layer — where business logic lives, separated from the web layer.
Practice quiz
What does @PathVariable bind?
- A query string value
- A segment of the URL path into a method parameter
- The request body
- An HTTP header only
Answer: A segment of the URL path into a method parameter. @PathVariable extracts a templated segment of the URL path (e.g. /users/{id}) into a method parameter.
Which annotation binds a query parameter like ?page=2?
- @PathVariable
- @RequestBody
- @RequestParam
- @ResponseBody
Answer: @RequestParam. @RequestParam binds query-string (or form) parameters such as ?page=2 to method arguments.
What does @RequestBody do?
- Reads a path segment
- Deserialises the HTTP request body (e.g. JSON) into an object
- Sets the status code
- Adds a header
Answer: Deserialises the HTTP request body (e.g. JSON) into an object. @RequestBody converts the incoming request body (typically JSON) into a Java object using a message converter.
For the route /users/{id}, how is id captured?
- @RequestParam Long id
- @RequestBody Long id
- @PathVariable Long id
- @RequestHeader Long id
Answer: @PathVariable Long id. A path template segment {id} is bound with @PathVariable Long id.
How do you make a @RequestParam optional with a default?
- @RequestParam(required=false) or defaultValue=...
- It cannot be optional
- Use @PathVariable
- Use @RequestBody
Answer: @RequestParam(required=false) or defaultValue=.... @RequestParam supports required=false and defaultValue to make a query parameter optional.
Which status code conventionally means a created resource?
- 200 OK
- 201 Created
- 404 Not Found
- 500 Internal Server Error
Answer: 201 Created. 201 Created indicates a new resource was successfully created, typically returned from a POST.
Which annotation sets a fixed status on a handler without ResponseEntity?
- @Scope
- @ResponseStatus
- @Qualifier
- @Primary
Answer: @ResponseStatus. @ResponseStatus(HttpStatus.CREATED) declares the status code a handler returns on success.
A request for a missing resource should typically return...
- 200 OK
- 201 Created
- 404 Not Found
- 301 Moved
Answer: 404 Not Found. 404 Not Found signals the requested resource does not exist.
For a JSON POST body mapped to an object, you use...
- @PathVariable
- @RequestParam
- @RequestBody
- @RequestHeader
Answer: @RequestBody. @RequestBody binds and deserialises the JSON request body into your DTO/record.
What does @RequestHeader bind?
- A URL path segment
- A named HTTP request header value
- The response body
- A cookie only
Answer: A named HTTP request header value. @RequestHeader binds the value of a named HTTP request header into a method parameter.