Difference between path and value attributes in @RequestMapping annotation

后端 未结 3 1561
旧巷少年郎
旧巷少年郎 2021-01-07 16:54

What is the difference between below two attributes and which one to use when?

@GetMapping(path = \"/usr/{userId}\")         


        
3条回答
  •  难免孤独
    2021-01-07 17:24

    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.

提交回复
热议问题