@PathVariable注解

发布时间 2023-10-04 16:44:39作者: 因为在乎

@PathVariable主要作用:映射URL绑定的占位符
带占位符的URL是 Spring3.0 新增的功能,URL中的 {xxx} 占位符可以通过 @PathVariable(“xxx”) 绑定到操作方法的入参中。
例如:

@RequestMapping("/user/{id}")
public String testPathVariable(@PathVariable("id") String id){
System.out.println("路径上的占位符的值="+id);
return "success";
}

假如你的请求为localhost:8080/user/admin,可以输出:

  路径上的占位符的值=admin