What is the difference between below two attributes and which one to use when?
@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"; }
value method is an alias for path method. This is an alias for path(). For example @RequestMapping("/foo") is equivalent to @RequestMapping(path="/foo"). So both methods are similar in that sense.
@RequestMapping — Multiple Paths Mapped to the Same Controller Method. Although a single @RequestMapping path value is usually used for a single controller method (just good practice, not a hard and fast rule), there are some cases where mapping multiple requests to the same method may be necessary.
@RequestMapping is used at the class level while @GetMapping is used to connect the methods. This is also an important Spring MVC interview question to knowing how and when to use both RequestMapping and GetMapping is crucial for Java developers.
annotation. RequestMapping annotation is used to map web requests onto specific handler classes and/or handler methods. @RequestMapping can be applied to the controller class as well as methods.
As mentioned in the comments (and the documentation), value
is an alias to path
. Spring often declares the value
element as an alias to a commonly used element. In the case of @RequestMapping
(and @GetMapping
, ...) this is the path
property:
This is an alias for
path()
. For example@RequestMapping("/foo")
is equivalent to@RequestMapping(path="/foo")
.
The reasoning behind this is that the value
element is the default when it comes to annotations, so it allows you to write code in a more concise way.
Other examples of this are:
@RequestParam
(value
→ name
)@PathVariable
(value
→ name
)However, aliases aren't limited to annotation elements only, because as you demonstrated in your example, @GetMapping
is an alias for @RequestMapping(method = RequestMethod.GET
).
Just looking for references of AliasFor
in their code allows you to see that they do this quite often.
@GetMapping
is a shorthand for @RequestMapping(method = RequestMethod.GET)
.
In your case. @GetMapping(path = "/usr/{userId}")
is a shorthand for @RequestMapping(value = "/usr/{userId}", method = RequestMethod.GET)
.
Both are equivalent. Prefer using shorthand @GetMapping
over the more verbose alternative. One thing that you can do with @RequestMapping
which you can't with @GetMapping
is to provide multiple request methods.
@RequestMapping(value = "/path", method = {RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT) public void handleRequet() { }
Use @RequestMapping
when you need to provide multiple Http verbs.
Another usage of @RequestMapping
is when you need to provide a top level path for a controller. For e.g.
@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 } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With