Difference between path and value in spring boot Requestmapping

Raj :

What is the difference between below two and which on 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";
}
g00glen00b :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=35383&siteId=1