@PathVariable
时间: 2023-10-03 09:04:14 浏览: 64
@PathVariable is an annotation used in Spring MVC framework to bind a method parameter to a URI template variable. It is used to extract values from the URL path and use them as method parameters.
For example, if we have a URL like `/users/{userId}` where userId is the variable part of the URL, we can use @PathVariable to extract the value of userId and use it in the method.
Here's an example:
```
@GetMapping("/users/{userId}")
public String getUser(@PathVariable("userId") Long userId) {
// do something with userId
return "user";
}
```
In this example, @GetMapping is used to map a GET request to the URL `/users/{userId}`. The method parameter userId is annotated with @PathVariable to extract the value of userId from the URL path. The value of userId is then used in the method to retrieve user information.
阅读全文