@pathvariable
时间: 2024-05-15 21:15:05 浏览: 66
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.
阅读全文