What is the difference between @RequestParam, @PathVariable, and @RequestBody?
Understand the difference between @RequestParam, @PathVariable and @RequestBody in Spring Boot with clear examples of query, path and JSON body binding.
Expected Interview Answer
@RequestParam binds query-string or form parameters, @PathVariable binds a segment of the URL path, and @RequestBody binds the entire HTTP request body (typically JSON) to a Java object.
@PathVariable extracts values embedded in the route such as the id in /users/42, and works with the URL template token declared in the mapping. @RequestParam reads key-value pairs after the ? or from form posts and supports defaults and optional flags. @RequestBody deserializes the whole payload via an HttpMessageConverter like Jackson into a POJO, used for create and update requests.
- Clear separation of path identity, filters, and payload data
- @RequestParam supports defaultValue and required flags
- @PathVariable keeps RESTful resource URLs clean
- @RequestBody enables automatic JSON-to-object deserialization
- Each integrates with @Valid for input validation
AI Mentor Explanation
Think of ordering from the team's kit room. @PathVariable is the jersey number that identifies exactly which player's kit, @RequestParam is the extra options you tick like size or long-sleeve, and @RequestBody is a full written order form describing a custom kit in complete detail.
Step-by-Step Explanation
Step 1
Identify the resource
Use @PathVariable for the identifier in the URL path, e.g. /orders/{orderId}.
Step 2
Add filters or options
Use @RequestParam for query parameters like ?page=2&size=20, with defaultValue and required as needed.
Step 3
Capture the payload
Use @RequestBody on the method parameter to deserialize the JSON body into a POJO for POST or PUT.
Step 4
Match method signatures
Ensure the @PathVariable name matches the URL template token in the mapping, or specify it explicitly.
Step 5
Validate inputs
Combine @RequestBody with @Valid, and range-check @RequestParam values before use.
What Interviewer Expects
- Knows path vs query vs body as three distinct request parts
- Can give a concrete URL showing each in action
- Understands @RequestBody uses HttpMessageConverters like Jackson
- Aware of @RequestParam defaultValue and required attributes
- Knows a handler can combine all three at once
Common Mistakes
- Using @RequestParam to read a value that is actually in the path
- Expecting @RequestBody to work on a GET with no body
- Forgetting to match the @PathVariable name to the URL template token
- Putting large payloads in query params instead of the body
- Not marking optional @RequestParam as required=false or giving a default
Best Answer (HR Friendly)
“These three annotations tell Spring where to find the data a request carries: @PathVariable pulls it from the web address itself, @RequestParam reads the options attached after the address, and @RequestBody takes the full form or document sent along. Picking the right one keeps the code correct and the API tidy.”
Code Example
@RestController
@RequestMapping("/api/orders")
public class OrderController {
@GetMapping("/{orderId}")
public Order getOrder(
@PathVariable Long orderId,
@RequestParam(defaultValue = "false") boolean includeItems) {
return orderService.find(orderId, includeItems);
}
@PostMapping
public Order createOrder(@RequestBody OrderRequest request) {
return orderService.create(request);
}
}Follow-up Questions
- How do you make a @RequestParam optional with a default value?
- What converter turns a JSON body into a Java object for @RequestBody?
- Can a single handler use @PathVariable, @RequestParam, and @RequestBody together?
- Why should large data go in the body rather than query parameters?
- How do you bind a path variable whose name differs from the parameter name?
MCQ Practice
1. Which annotation binds the id in the URL /users/42?
42 is a segment of the path, so @PathVariable extracts it.
2. Which annotation reads ?page=2 from the query string?
@RequestParam binds query-string and form parameters like page=2.
3. Which annotation deserializes a JSON payload into a POJO?
@RequestBody uses an HttpMessageConverter (e.g. Jackson) to map the body to an object.
Flash Cards
@PathVariable binds what? — A value embedded in the URL path, such as the id in /users/{id}.
@RequestParam binds what? — Query-string or form parameters, with optional defaultValue and required flags.
@RequestBody binds what? — The full HTTP request body, deserialized into a Java object via a message converter.
Which converter powers @RequestBody for JSON? — Jackson, wired in as an HttpMessageConverter by Spring Boot.
Can one handler use all three? — Yes — a method can mix @PathVariable, @RequestParam, and @RequestBody parameters.