@PathVariable vs @RequestParam — URL Path vs Query String
Fast recall
Quick revision
Core answer
REST API design understanding — this is a classic comparison question that reveals whether a candidate knows RESTful URL conventions
Using @RequestParam for resource IDs that should be @PathVariable, resulting in non-RESTful URLs like /users?id=42 instead of /users/42
Articulate the REST design principle: path for nouns and resource identity, query for adjectives and filtering or modification
Interview-ready guide
Interview answer
Use /orders/900 when 900 identifies the order resource. Use /orders?status=PAID&sort=createdAt for a filtered collection. A lookup endpoint /orders?externalRef=ABC can still use a required query parameter when that is the search contract. A nested /users/42/orders/900 route still needs code to verify that order 900 belongs to user 42 and that the caller may see it.
For example, /orders/42 maps 42 with @PathVariable to one order, while /orders?status=PAID maps status with @RequestParam to an optional filter over the order collection.
@PathVariable vs @RequestParam — URL Path vs Query String
Direct Answer
@PathVariable reads a named segment captured by a URI template, while @RequestParam reads a request parameter such as a query-string value. Resource identifiers often belong in paths and collection filters/options often belong in query parameters, but required versus optional is configured independently. Neither annotation provides authorization or validates resource ownership.
Code Example
@RestController
@RequestMapping("/users")
class UserController {
// PATH VARIABLE — identifies WHICH resource: GET /users/42
@GetMapping("/{id}")
public User getById(@PathVariable Long id) {
return userService.findById(id);
}
// REQUEST PARAM — filters/options: GET /users?status=ACTIVE&page=0
@GetMapping
public List<User> search(@RequestParam String status,
@RequestParam(defaultValue = "0") int page) {
return userService.findByStatus(status, page);
}
}
Path = which resource; query = how to filter it. They often combine: GET /users/42/orders?status=PAID.
Common Mistakes & Pitfalls
- Using @RequestParam for IDs: Freshers write
@GetMapping("/users") getUserById(@RequestParam Long id), resulting in/users?id=42. This breaks REST conventions. Resource IDs should be in the path:/users/42with@PathVariable. - Mixing them incorrectly: Using path variables for filters like
/users/ACTIVE/ADMINinstead of/users?status=ACTIVE&role=ADMIN. Path variables force a rigid hierarchy; query params give flexibility. - Not understanding required defaults: Both annotations require parameters by default, but the failure mode differs — missing path variables return 404, missing query params return 400. Freshers don't anticipate this when designing APIs that should accept optional filters.
Check your understanding
Answer these without looking back at the article.
- When would you use @PathVariable for something other than an ID?
- How do you design a REST URL for a nested resource (user's orders)?
- Can you use both @PathVariable and @RequestParam in the same endpoint?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.