@PathVariable
时间: 2023-10-03 12:04:14 浏览: 39
@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.
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)