@PathVariable 注解用于从request中接受参数
@PathVariable绑定URI模板变量值
@PathVariable是用来获得请求url中的动态参数的
@PathVariable用于将请求URL中的模板变量映射到功能处理方法的参数上。
@GetMapping("findUserByUserId/{id}")
public User findUserByUserId(@PathVariable("id") Integer id) {
return userService.findUserByUserId(id);
}
以上url模板是findUserByUserId/{id},id作为参数。
@PathVariable is different from @RequestParam.
Which one to choose?
当URL指向某一具体业务资源(或者资源列表),如博客,使用@PathVariable
当URL需要对资源/资源列表进行过滤、筛选时,使用@RequestParam
For example:
/blogs/{blogId} --> 显示id = blogId的博客
/blogs?state=publish --> 正确 /blogs/state/publish --> 错误 使用state参数来过滤显示博客。