@PathVariable works together with mapping annotations like @RequestMapping, @GetMapping, and others by extracting dynamic segments from the same URL pattern those annotations declare. The mapping annotation defines where the placeholder sits in the path, and @PathVariable pulls its value into the handler method.
A method mapped with @GetMapping("/orders/{orderId}/items/{itemId}") can declare both @PathVariable Long orderId and @PathVariable Long itemId, letting one handler serve any combination of order and item IDs.
@GetMapping("/orders/{orderId}/items/{itemId}")
public Item getItem(@PathVariable Long orderId, @PathVariable Long itemId) {
return orderService.findItem(orderId, itemId);
}A concise interview answer is
@PathVariable pairs directly with the placeholders declared in @RequestMapping or @GetMapping — the mapping defines the URL pattern, and @PathVariable extracts the matching segments into method parameters. You can combine several path variables with query parameters or a request body in the same handler.
Last updated: 11 November 2026
Previous question: What are the considerations when using @PathVariable in terms of URL design?
Next question: How does Spring MVC use LocaleResolver?