简体   繁体   中英

@RequestParam omitted but mapping is still done correctly

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

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.

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.

@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

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); } @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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM