@RequestMapping注解中path属性和value属性的区别
Difference between path and value attributes in @RequestMapping annotation
下面两个属性有什么区别,什么时候使用哪个?
@GetMapping(path = "/usr/{userId}")
public String findDBUserGetMapping(@PathVariable("userId") String userId) {
return "Test User";
}
@RequestMapping(value = "/usr/{userId}", method = RequestMethod.GET)
public String findDBUserReqMapping(@PathVariable("userId") String userId) {
return "Test User";
}
@GetMapping 是@RequestMapping
的别名
@GetMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.GET).
value方法是路径方法的别名。
This is an alias for path(). For example @RequestMapping("/foo") is equivalent to @RequestMapping(path="/foo").
所以这两种方法在这个意义上是相似的。
如评论中所述(和 the documentation),value
是 path
的别名。 Spring 通常将 value
元素声明为常用元素的别名。在 @RequestMapping
(和 @GetMapping
,...)的情况下,这是 path
属性:
This is an alias for path()
. For example @RequestMapping("/foo")
is equivalent to @RequestMapping(path="/foo")
.
这背后的原因是 value
元素是注释的默认元素,因此它允许您以更简洁的方式编写代码。
其他例子是:
@RequestParam
(value
→ name
)
@PathVariable
(value
→ name
)
- ...
但是,别名不仅限于注释元素,因为正如您在示例中所演示的,@GetMapping
是 @RequestMapping(method = RequestMethod.GET
) 的别名。
只需查找 references of AliasFor
in their code,您就会发现他们经常这样做。
@GetMapping
是 shorthand 对于 @RequestMapping(method = RequestMethod.GET)
。
在你的情况下。
@GetMapping(path = "/usr/{userId}")
是 @RequestMapping(value = "/usr/{userId}", method = RequestMethod.GET)
的 shorthand。
两者是等价的。更喜欢使用 shorthand @GetMapping
而不是更冗长的替代方法。 @RequestMapping
可以做而 @GetMapping
不能做的一件事是提供多种请求方法。
@RequestMapping(value = "/path", method = {RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT)
public void handleRequet() {
}
当您需要提供多个 Http 动词时使用 @RequestMapping
。
@RequestMapping
的另一种用法是当您需要为控制器提供顶级路径时。例如
@RestController
@RequestMapping("/users")
public class UserController {
@PostMapping
public void createUser(Request request) {
// POST /users
// create a user
}
@GetMapping
public Users getUsers(Request request) {
// GET /users
// get users
}
@GetMapping("/{id}")
public Users getUserById(@PathVariable long id) {
// GET /users/1
// get user by id
}
}
下面两个属性有什么区别,什么时候使用哪个?
@GetMapping(path = "/usr/{userId}")
public String findDBUserGetMapping(@PathVariable("userId") String userId) {
return "Test User";
}
@RequestMapping(value = "/usr/{userId}", method = RequestMethod.GET)
public String findDBUserReqMapping(@PathVariable("userId") String userId) {
return "Test User";
}
@GetMapping 是@RequestMapping
的别名@GetMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.GET).
value方法是路径方法的别名。
This is an alias for path(). For example @RequestMapping("/foo") is equivalent to @RequestMapping(path="/foo").
所以这两种方法在这个意义上是相似的。
如评论中所述(和 the documentation),value
是 path
的别名。 Spring 通常将 value
元素声明为常用元素的别名。在 @RequestMapping
(和 @GetMapping
,...)的情况下,这是 path
属性:
This is an alias for
path()
. For example@RequestMapping("/foo")
is equivalent to@RequestMapping(path="/foo")
.
这背后的原因是 value
元素是注释的默认元素,因此它允许您以更简洁的方式编写代码。
其他例子是:
@RequestParam
(value
→name
)@PathVariable
(value
→name
)- ...
但是,别名不仅限于注释元素,因为正如您在示例中所演示的,@GetMapping
是 @RequestMapping(method = RequestMethod.GET
) 的别名。
只需查找 references of AliasFor
in their code,您就会发现他们经常这样做。
@GetMapping
是 shorthand 对于 @RequestMapping(method = RequestMethod.GET)
。
在你的情况下。
@GetMapping(path = "/usr/{userId}")
是 @RequestMapping(value = "/usr/{userId}", method = RequestMethod.GET)
的 shorthand。
两者是等价的。更喜欢使用 shorthand @GetMapping
而不是更冗长的替代方法。 @RequestMapping
可以做而 @GetMapping
不能做的一件事是提供多种请求方法。
@RequestMapping(value = "/path", method = {RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT)
public void handleRequet() {
}
当您需要提供多个 Http 动词时使用 @RequestMapping
。
@RequestMapping
的另一种用法是当您需要为控制器提供顶级路径时。例如
@RestController
@RequestMapping("/users")
public class UserController {
@PostMapping
public void createUser(Request request) {
// POST /users
// create a user
}
@GetMapping
public Users getUsers(Request request) {
// GET /users
// get users
}
@GetMapping("/{id}")
public Users getUserById(@PathVariable long id) {
// GET /users/1
// get user by id
}
}