Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java annotations in odd places

Browsing through the source of a spring framework project i came across a method that looked like this:

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

Being only a casual java user, ive not come across this before. As far as i was aware, the @ symbol preceded java annotations, yet there appear to be annotations in the method signature itself. What are the @ResponseBody and @RequestParam sections doing?

like image 656
richzilla Avatar asked Sep 09 '26 19:09

richzilla


2 Answers

The @ResponseBody is actually just a Plain-Jane Method annotation. You're allowed to put them after the scope keyword.

The @RequestParam annotation isn't part of the method signature. It's a Parameter Annotation.

like image 89
Tim Pote Avatar answered Sep 11 '26 09:09

Tim Pote


Those annotations are specific to Spring, so you'll need to dive into Spring to learn all the annotations and what they mean.

Whenever I need to learn something new in Spring, I always jump back to the documentation: http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/

It's good to read through that whole thing at least once in your life, if you plan on using Spring for enterprise development.

like image 33
kevin628 Avatar answered Sep 11 '26 08:09

kevin628