Java annotations in odd places

Looking through the source code for the spring project, I came across a method that looks like this:

@RequestMapping("primitive")
public @ResponseBody String primitive(@RequestParam Integer value) {
    return "Converted primitive " + value;
}

Being just a random java user, I haven't come across this before. As far as I know, the @ symbol precedes java annotations, but there are annotations in the method signature itself. What are the sections @ResponseBodyand @RequestParam?

+3
source share
3 answers

@ResponseBodyactually just a summary of the Plain-Jane method . You can put them after the scope keyword.

Annotation is @RequestParamnot part of the method signature. These are parameter annotations .

+10
source

Java. , - @Target. , , . @Target ElementType, .

Java 6, ElementType :

ANNOTATION_TYPE, CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETERAndTYPE

In your example, @RequestMappingthey @ResponseBodyare level annotations METHOD, although they are not applied in the same place (one before the method area modifier and one after it), but @RequestParama PARAMETERlevel annotations.

0
source

All Articles