@PathVariable
时间: 2023-10-03 21:03:20 浏览: 32
@PathVariable是Spring框架中的注解,用于从URL中提取参数值。在Spring MVC中,@PathVariable注解可以用于获取RESTful URL中的参数,例如:
```java
@GetMapping("/users/{id}")
public User getUserById(@PathVariable Long id) {
// 根据id获取用户信息并返回
}
```
在上述示例中,@GetMapping注解用于映射HTTP GET请求,"/users/{id}"表示RESTful URL,其中的{id}是一个变量,用@PathVariable注解来获取该变量的值,并将其作为方法参数传入。
相关问题
@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.
阅读全文