Spring @PathVariable
Learn how @PathVariable works in Spring Boot with RESTful URL design, resource IDs, UUID path variables, validation, and API examples.
Last reviewed: November 2026
🔍 What Is It?
@PathVariable binds a URI template variable to a method parameter.
Syntax:
- →Define a template variable:
@GetMapping("/users/{id}") - →Bind it:
@PathVariable("id") Long id - →Or with explicit name:
@PathVariable("id") Long userId
Supported types:
- →
String,int,long,Integer,Long - →
UUID,LocalDate - →Other types supported by the configured conversion service; register a converter for a custom identifier when needed
Use an explicit annotation name even when it matches the Java parameter. Omitting it relies on retained parameter-name metadata, such as compilation with -parameters; parameter-name discovery changed in Spring Framework 6.1.
Request outcomes
| Request | Mapping and parameter | Typical result |
|---|---|---|
GET /users/42 | /users/{id}, Long id | id is 42 |
GET /users/abc | /users/{id}, Long id | 400 from type conversion |
GET /users | Only /users/{id} exists | Mapping does not match; normally 404 if no other handler matches |
GET /users/42 | Conversion succeeds, service finds no user | Application must return the appropriate response, usually 404 |
Conversion is separate from existence and authorization. A valid ID or UUID does not prove the caller can access the resource. A negative Long also converts successfully; enforce domain constraints separately.
Multiple path variables:
@GetMapping("/users/{userId}/orders/{orderId}")
public OrderDto getOrder(
@PathVariable("userId") Long userId,
@PathVariable("orderId") Long orderId) { ... }🎯 Why Do We Need It?
RESTful API design uses URL paths to identify resources directly. /users/42 is clearer and more semantic than /users?id=42 when the request is about one specific entity. @PathVariable makes resource identification explicit in the URL structure and keeps Spring Boot controller mappings aligned with REST conventions.
🏗️ What Happens in Real Projects?
Every CRUD API uses path variables for resource identification:
- →
GET /api/v1/orders/{orderId}— get specific order - →
PUT /api/v1/users/{userId}/roles/{roleId}— assign role to user - →
DELETE /api/v1/products/{productId}— delete product - →
GET /api/v1/categories/{categorySlug}/products— products in category
⚙️ How It Works
Spring first matches the request path against the mapping, then resolves each URI variable by name and converts the captured text to the declared Java type. A conversion failure normally produces a 400 response before the controller body runs. Once conversion succeeds, the application still needs to perform domain validation, lookup, and access checks.
Optional path variables
required = false changes argument resolution; it does not make a missing URL segment match an existing pattern. Map both /users and /users/{id} if the same handler intentionally accepts both. Use a wrapper type or Optional for the missing value.
Dates and slash-containing paths
Use an explicit date format for a LocalDate argument. A normal {id} captures one path segment. Multi-segment capture such as {*path} depends on the configured PathPattern matching strategy and belongs at the end of a pattern; it is not an optional-segment syntax.
💻 Example
Path Variables in Practice — Single, multiple, and optional path variables.
🤔 Common Confusions
❓ What if the path variable doesn't match the method parameter name?
Use @PathVariable("urlVariableName") Type methodParamName. Example: @GetMapping("/users/{user_id}") with @PathVariable("user_id") Long userId. The names must match when you omit the annotation value — Spring uses the parameter name by default.
❓ Can path variables be optional?
Yes, with required=false: @PathVariable(required=false) Long id. But this requires two mappings: one with the variable and one without. Better approach: use two separate methods with different @GetMapping paths, or use @RequestParam for optional identifiers.
🚨 Common Production Issues
Path variable not validated — a negative or zero ID reaching the database; add @Positive validation
String path variable containing SQL injection attempts — never use path variables directly in SQL strings
UUID path variable causing 400 errors for invalid UUID format — handle MethodArgumentTypeMismatchException
✅ Best Practices
Use Long or UUID for IDs — not String (prevents type mismatch errors reaching your service)
Validate path variable values: @PathVariable @Positive Long id
Handle MethodArgumentTypeMismatchException in your global exception handler
Use semantic slugs for public-facing APIs (/products/iphone-15-pro) instead of numeric IDs
🏛️ Architect's Note
Choose identifiers according to the resource contract. UUIDs can make guessing identifiers harder, but do not replace authorization. For a nested URL such as /users/{userId}/orders/{orderId}, verify both that the order belongs to that user and that the caller may access it. Human-readable slugs can help public navigation, but need an explicit strategy when names change.
💬 Frequently Asked Questions
🎯 Key Takeaways
🔗 Related Topics
🎯 Practice this topic in interviews
↗ Where this topic fits
Spring Boot Technology Guide
Return to the Spring Boot learning hub.
Backend Engineer Career Path
A structured journey through Core Java, Spring Boot, production backend engineering, and interview preparation.
Full Stack Java Developer Career Path
A guided route through frontend foundations, Java backend engineering, delivery workflows, and interview readiness.
Sources
Sources / References
Use these Spring Boot references to verify the concept in official docs and continue into the most relevant SplashRide pages.
Official documentation
Related SplashRide pages
SplashRide Spring Boot learning path
Browse the full Spring Boot topic collection and learning order on SplashRide.
Spring Boot Interview Prep
Practice Spring Boot interview questions and scenario-based answers.
Backend Engineer Career Path
A structured journey through Core Java, Spring Boot, production backend engineering, and interview preparation.
Full Stack Java Developer Career Path
A guided route through frontend foundations, Java backend engineering, delivery workflows, and interview readiness.
Controllers
Master Spring MVC Controllers — the web layer that handles HTTP requests, maps URLs to methods, and returns responses in enterprise REST APIs.
Request Mapping
Master Spring MVC request mapping - how to map HTTP requests to controller methods using @RequestMapping, @GetMapping, @PostMapping, and the full range of mapping options.