@PathVariable注解的作用

@PathVariable 映射 URL 绑定的占位符

通过 @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中:URL 中的 {xxx} 占位符可以通过

@PathVariable("xxx") 绑定到操作方法的入参中。

一般与@RequestMapping(method = RequestMethod.GET)一起使用

@RequestMapping("/getUserById/{name}")
    public User getUser(@PathVariable("name") String name){
        return userService.selectUser(name);
    }
若方法参数名称和需要绑定的uri中变量名称一致时,可以简写:
@RequestMapping("/getUser/{name}")
    public User getUser(@PathVariable String name){
        return userService.selectUser(name);
    }

猜你喜欢

转载自www.cnblogs.com/liqbk/p/13188864.html