What is @PathVariable in Spring Boot?
@PathVariable is one of the most commonly used annotations in Spring Boot REST APIs.
It is used to:
- Read values from URL path
- Capture dynamic URL parameters
- Pass resource identifiers like IDs
It helps create clean and RESTful URLs.
Simple Definition
@PathVariable is used to extract values from URL path and bind them to method parameters.
@PathVariable = Reads dynamic values from URL path.
Why is @PathVariable Needed?
In REST APIs, resources are identified using URLs.
Example:
/api/courses/1
Here:
- 1 is dynamic value
- Represents course ID
@PathVariable extracts this value automatically.
Basic Example
@GetMapping("/courses/{id}")
public Course getCourse(
@PathVariable Long id) {
return courseService.getById(id);
}
If request is:
GET /courses/10
Then:
id = 10
Spring automatically maps URL value to method parameter.
Real-Time Example
Suppose we are developing Dhanish Empower.
Frontend wants course details.
Frontend sends:
GET /api/courses/5
Here:
- 5 is course ID
- @PathVariable captures it
How @PathVariable Works Internally
- Client sends HTTP request.
- Spring matches URL pattern.
- Dynamic value is extracted.
- Value is converted into required datatype.
- Method parameter receives value.
Internal Flow Diagram
Client Request
↓
URL Pattern Matching
↓
@PathVariable Extraction
↓
Datatype Conversion
↓
Controller Method
URL Mapping Example
@GetMapping("/users/{id}")
URL:
/users/100
Extracted Value:
id = 100
Example with Multiple Path Variables
@GetMapping("/users/{userId}/orders/{orderId}")
public String getOrder(
@PathVariable Long userId,
@PathVariable Long orderId) {
return "User: " + userId
+ ", Order: " + orderId;
}
Request:
/users/10/orders/200
Result:
userId = 10
orderId = 200
Custom Variable Name Example
@GetMapping("/courses/{courseId}")
public Course getCourse(
@PathVariable("courseId") Long id) {
return courseService.getById(id);
}
Here:
- URL variable = courseId
- Method parameter = id
Supported Datatypes
@PathVariable supports:
- String
- Long
- Integer
- UUID
- Custom datatypes
@PathVariable with PUT Example
@PutMapping("/courses/{id}")
public Course updateCourse(
@PathVariable Long id,
@RequestBody Course course) {
return courseService.update(id, course);
}
Here:
- @PathVariable reads ID
- @RequestBody reads JSON data
@PathVariable vs @RequestParam
| @PathVariable | @RequestParam |
|---|---|
| Reads URL path values | Reads query parameters |
| Used for resource identifiers | Used for filters/search |
| /courses/1 | /courses?id=1 |
@PathVariable vs @RequestBody
| @PathVariable | @RequestBody |
|---|---|
| Reads URL values | Reads JSON request body |
| Used for IDs | Used for complex objects |
REST API Design Best Practice
Use resource IDs in path variables:
/api/courses/1
instead of:
/api/getCourse?id=1
because path variables create cleaner RESTful URLs.
Common Errors
1. Variable Name Mismatch
@GetMapping("/courses/{id}")
public Course getCourse(
@PathVariable Long courseId)
Error occurs because:
- URL variable = id
- Method parameter = courseId
Fix:
@PathVariable("id") Long courseId
2. Invalid Datatype
Example:
/courses/abc
when expecting:
Long id
causes datatype conversion error.
Advantages of @PathVariable
- Creates clean RESTful URLs
- Easy resource identification
- Improves API readability
- Simplifies URL handling
- Supports dynamic URLs
- Improves frontend-backend communication
Production-Level Importance
@PathVariable is heavily used in:
- REST APIs
- Microservices
- Banking systems
- E-commerce platforms
- Cloud-native applications
Almost every enterprise REST API uses path variables.
Interview Answer
@PathVariable is used in Spring Boot to extract values from URL paths and bind them to method parameters.
It is commonly used to identify resources such as IDs.
Example:
/api/courses/1
where 1 is extracted using @PathVariable.
Short Interview Answer
@PathVariable extracts dynamic values from URL path.
Frequently Asked Questions
What is the purpose of @PathVariable?
To read dynamic values from URL paths.
Can multiple path variables be used?
Yes.
Which HTTP methods commonly use @PathVariable?
GET, PUT, DELETE.
What is difference between @PathVariable and @RequestParam?
@PathVariable reads URL path values, while @RequestParam reads query parameters.
Can @PathVariable work with different datatypes?
Yes, Spring automatically converts datatypes.
Conclusion
@PathVariable is one of the most important annotations in Spring Boot REST API development.
It helps create clean RESTful URLs, improves API readability, and simplifies resource identification.
It is widely used in enterprise applications, microservices, and cloud-native systems.