java / beginner
Snippet
Extracting Path Variables
The @PathVariable annotation is used to extract values from the URI path. This is useful for creating RESTful URLs where the resource identifier is part of the path itself.
snippet.java
1
2
3
4
@GetMapping("/users/{id}")public String getUser(@PathVariable("id") Long id) {return "User ID: " + id;}
spring
Breakdown
1
@GetMapping("/users/{id}")
Defines a dynamic segment '{id}' in the URL path.
2
@PathVariable("id")
Binds the path segment to the method argument.