@PathVariable
时间: 2023-10-17 10:37:06 浏览: 22
@PathVariable是Spring框架中的注解,用于将请求路径中的参数绑定到控制器方法的参数上。例如,如果请求路径为"/users/{userId}",那么可以在控制器方法的参数上使用@PathVariable("userId")注解来获取该参数的值。这个注解可以方便地获取RESTful风格的请求参数。
相关问题
@pathvariable
PathVariable is a Spring annotation used to extract values from the URI (Uniform Resource Identifier) path. It is used to bind a URI template variable to a method parameter in a Spring MVC controller.
For example, if you have a URI `http://example.com/api/person/{id}`, you can use `@PathVariable` to extract the `id` value from the URI path and pass it as a parameter to your Spring MVC controller method.
Here's an example of how to use `@PathVariable` in a Spring MVC controller:
```
@RestController
@RequestMapping("/api/person")
public class PersonController {
@GetMapping("/{id}")
public Person getPersonById(@PathVariable Long id) {
// code to retrieve and return person object with given id
}
}
```
In this example, the `@PathVariable` annotation is used to extract the `id` value from the URI path and pass it as a parameter to the `getPersonById` method in the `PersonController` class.
阅读全文