Lives in the UK. Likes blogging, cycling and eating lemon drizzle cake. Roger is a DZone MVB and is not an employee of DZone and has posted 143 posts at DZone. You can read more from them at their website. View Full User Profile

A Footnote on Accessing Request Parameters using Spring 3 MVC

  • submit to reddit
I recently got a comment on my blog: Accessing Request Parameters using Spring 3 MVC, pointing out something that I didn’t know about Spring’s @RequestParam annotation. The comment, from salient1, went like this:

“Most of your examples don't actually require you to use the @RequestParam annotation. You could have just created a String parameter called uuid and Spring would still automap it for you. You only need the annotation if you want your variable be different than the name of the value passed in or you want to change the default value, etc. Otherwise, it's a waste of space. on Accessing Request Parameters using Spring 3 MVC

So, I took my original code and tried this out1 and, yes, he’s right. If the argument name is the same as the request parameter name, then Spring will automatically do the mapping for you without the need for the @RequestParm annotation. This means that my original code example:

  @RequestMapping(value = "/help/detail", method = RequestMethod.GET)
  public String displaySomeHelpDetail(@RequestParam("uuid") String uuid, Model model) {
    return "";

...could be written like this:
  @RequestMapping(value = "/help/detail", method = RequestMethod.GET)
  public String displaySomeHelpDetail(String uuid, Model model) {
    return "";

This doesn’t really come as a big surprise, after all, Spring automatically maps other request handler arguments, such as Model without you having to do anything, although I do have a couple of reservations on using this technique.

Firstly, my blog, Accessing Request Parameters using Spring 3 MVC, has had about 10,000 hits and salient1 is the only person to mention this fact. This means that either lots of people know about this technique and haven’t told me, or it there’re very few people who know about it. I’m guessing that it’s the latter case which it true. Therefore, if only 1 in 10,000 developers knows about this feature then you have to question whether it’s a good idea to use it, especially given that one of the aims of any good developer should be to write clear and maintainable code. My first example above, although more verbose and containing a redundant annotation, will be understood by more developers than my second more concise example. You could therefore argue that that my first example is more readable, understandable and therefore maintainable than the second.

Secondly, I had a quick trawl through the Spring documentation on this and couldn’t find this technique mentioned anywhere2. This means that it is probably an undocumented feature and it’s NEVER a good idea to use undocumented features. They can be changed or removed at anytime without anything being said by the developers. This means that your code will stop working without any explanation if you, for example in this case, upgraded your version of Spring.

On the other hand, you could use this little known technique to impress your more junior colleagues as it’s one of those little known facts3 that will make you appear to be your team’s super cool Spring guru.

1This was tested using Spring 3.1
2If anyone knows where it’s mentioned then please send me the link.
3If you have heard of this technique, please let me know.
Published at DZone with permission of Roger Hughes, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Gundar Abella Posse replied on Thu, 2012/10/11 - 3:29am

What I use to do is write params this way:

@RequestParam String uuid (Note there is no name for param in the annotation).

It worked like a charm in windows, but a coworker tried to test it in linux and it failed in every single controller method. I had to change all methods and write a name for every param:

@RequestParam(“uuid”) String uuid

Arnauld Loyer replied on Thu, 2012/10/11 - 8:10am

Parameter's name is usually not accessible at runtime unless the code was compiled with the -g option.

According to this, it would be better to not rely on it. 

 For more information, have a look there: 

Francesco Scoccia replied on Thu, 2012/10/11 - 9:39am

Saw this feature mentioned in the Sping in Action Third edition book,  and more or less that guide has been out for a year

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.