Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is @RequestParam annotation not needed in Spring MVC?

I'm looking through some Spring 3 MVC controller code and I see that @RequestParam is used for some parameters and not for others. Example where it is not being used:

@RequestMapping(value = "/experiments", method = RequestMethod.GET)
public String getExperimentsPage(ExperimentSearchCriteria criteria, Map<String, Object> model) {
    // method body here
}

When is @RequestParam (or similar parameter-specifying annotation) not needed?

like image 820
Victor Lyuboslavsky Avatar asked Apr 25 '13 03:04

Victor Lyuboslavsky


1 Answers

Good question, I have been wondering this too until I found it being mentioned in the doc:

Note that use of @RequestParam is optional, e.g. to set its attributes. By default any argument that is a simple value type, as determined by BeanUtils#isSimpleProperty, and is not resolved by any other argument resolver, is treated as if it was annotated with @RequestParam.

https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann-requestparam

Most of time, I don't specify this unless my method parameter name is different with request parameter, or, if the value is optional, I will need to use required=false.

like image 149
Sam YC Avatar answered Nov 09 '22 23:11

Sam YC