@pathvariable
时间: 2023-09-09 18:10:23 浏览: 57
@RequestMapping 中的 @PathVariable 注解用于将 URL 中的变量绑定到方法的参数上。例如:
```
@GetMapping("/users/{userId}")
public User getUser(@PathVariable Long userId) {
return userService.getUserById(userId);
}
```
在上面的示例中,@PathVariable 注解将 URL 中的 userId 变量绑定到 getUser() 方法的 userId 参数上,使得我们可以通过 userId 参数获取对应的用户信息。
相关问题
@PathVariable
@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.
阅读全文