In some cases I have seen two different ways to specify uri in @RequestMapping:

  1. @RequestMapping(value="/somepath")

  2. @RequestMapping("/somepath")

The latter is often used at class level. So, is it always legal omitting the value attribute?

有帮助吗?

解决方案

The Java Language Spec states

It is legal to use single-element annotations for annotation types with multiple elements, so long as one element is named value, and all other elements have default values.

Since @RequestMapping has a defined value attribute and all other attributes have default values (empty arrays), it is always legal to omit spelling the name value if you don't specify any other attributes for the annotation:

@RequestMapping("/somepath") // valid
@RequestMapping("/somepath",  method = RequestMethod.GET) // INVALID!

其他提示

For @RequestMapping annotation value is the primary attribute, it is mandatory.

1)@RequestMapping(value="/somepath"): Here you can mention remaining attributes, for ex: method,produces 2) @RequestMapping("/somepath"): Here you can not mention rest of the attributes, as by default "/somepath" will be taken as value attribute.

Check this

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top