Question

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?

Was it helpful?

Solution

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!

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top