0 day streak
java / beginner
Snippet

Dynamic Routing with @PathVariable

Use @PathVariable to extract dynamic values directly from the URL path. This is standard for RESTful APIs when identifying specific resources.

snippet.java
java
1
2
3
4
@GetMapping("/users/{id}")
public User getUser(@PathVariable("id") Long userId) {
return userService.findById(userId);
}
spring
Breakdown
1
@GetMapping("/users/{id}")
Defines a GET route where {id} is a dynamic placeholder.
2
@PathVariable("id") Long userId
Binds the value from the URL placeholder {id} to the method parameter userId.