Pergunta

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?

Foi útil?

Solução

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!

Outras dicas

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top