Reputation: 12819
I just found that even if I omit the @RequestParam
annotation on the organization
parameter, Spring is still able to bind it.
@RequestMapping(value="", method = RequestMethod.POST) @ResponseBody public String save(String organization){ logger.info(organization); // it works }
Can anyone points to the documentation that clarifies this behaviour? I have always though that @RequestParam
was mandatory for binding to work.
Thanks
Upvotes: 4
Views: 1358
Reputation: 1
here I have some examples for the @RequestParam to provide you,hope they can help you:
@RequestMapping(value = "/selection/findByField", method = RequestMethod.POST)
public @ResponseBody List<selectionsDO> add(@RequestParam(value = "field", required = true) String field,@RequestParam(value = "value", required = true) String value)
{ return mongoService.findByField(field,value);
}
The words "required = true" means that this field must submit at request.
Upvotes: -2
Reputation: 105
Your resolvers do it automatically. When you pass the HandlerMethodArgumentResolver
bean to your resolver, the BeanUtil
checks if the parameter is a primitive value or a simple String
. If so, it does the binding itself.
Upvotes: 1
Reputation: 1764
@RequestMapping(value = "/rest")
@ResponseBody
public String save(String username, String password) {
return String.format("username=%s password=%s", username, password);
}
Hit the service http://localhost:8080/rest?username=mypwd&password=uname
You will be able to see the result given below.
Output: username=pwd password=uname
Upvotes: -2
Reputation: 534
Take a look at https://reversecoding.net/spring-mvc-requestparam-binding-request-parameters/ There is an explanation:
Examples without @RequestParam
Based on the list of HandlerMethodArgumentResolver configured in your application, @RequestParam can also be omitted. If you have a look at the code of method getDefaultArgumentResolvers() of RequestMappingHandlerAdapter there is the following piece of code at the end:
// Catch-all resolvers.add(new RequestParamMethodArgumentResolver(getBeanFactory(), true));
resolvers.add(new ServletModelAttributeMethodProcessor(true));
// Catch-all resolvers.add(new RequestParamMethodArgumentResolver(getBeanFactory(), true));
resolvers.add(new ServletModelAttributeMethodProcessor(true));
Basically, it’s added to the resolvers a RequestParamMethodArgumentResolver with useDefaultResolution set to true. Looking at the documentation we can see that this means that method argument that is a simple type, as defined in BeanUtils.isSimpleProperty(java.lang.Class), is treated as a request parameter even if it isn’t annotated. The request parameter name is derived from the method parameter name.
Upvotes: 6