@pathvariable注解的使用

带占位符的 URL 是 Spring3.0 新增的功能,该功能在SpringMVC 向 REST 目标挺进发展过程中具有里程碑的意义。

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

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

    //@GetMapping是一个组合注解,是@RequestMapping(method = RequestMethod.GET)的缩写
    @GetMapping("/getUserById/{id}")
    public User getUser(@PathVariable("id") Long userId){
        return userService.selectUserById(userId);
    }
 
 
不需要使用问号传参,不需要写key=valuel,直接写value即可.
若方法参数名称和需要绑定的uri template中变量名称一致时,可以简写:
    //@GetMapping是一个组合注解,是@RequestMapping(method = RequestMethod.GET)的缩写。
    @GetMapping("/getUserByName/{userName}")
    public User getUserByName(@PathVariable String userName){
        return userService.selectUserByUserName(userName);
    }



猜你喜欢

转载自blog.csdn.net/qq_39940205/article/details/80262927