58

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";
}
informatik01
  • 16,038
  • 10
  • 74
  • 104
Raj
  • 2,463
  • 10
  • 36
  • 52
  • 1
    According to the [doc](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html#value--), they're the same : "This is an alias for path(). For example @RequestMapping("/foo") is equivalent to @RequestMapping(path="/foo"). " – Guillaume Ruchot May 15 '18 at 13:36

3 Answers3

43

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 (valuename)
  • @PathVariable (valuename)
  • ...

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.

g00glen00b
  • 41,995
  • 13
  • 95
  • 133
20

@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
    }
}
Juzer Ali
  • 4,109
  • 3
  • 35
  • 62
  • 2
    @Juzer Ali `@GetMapping(path = "/usr/{userId}")`, `@GetMapping(value = "/usr/{userId}")` and `@GetMapping("/usr/{userId}")` all are same? – Antony Vimal Raj Jun 19 '19 at 06:25
8

@GetMapping is an alias for @RequestMapping

@GetMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.GET).

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.

Ori Marko
  • 56,308
  • 23
  • 131
  • 233