简体   繁体   English

@RequestParam省略,但映射仍正确完成

[英]@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. 我只是发现,即使我在organization参数上省略了@RequestParam批注,Spring仍然能够绑定它。

@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. 我一直都认为@RequestParam是绑定工作所必需的。

Thanks 谢谢

Take a look at https://reversecoding.net/spring-mvc-requestparam-binding-request-parameters/ There is an explanation: 看看https://reversecoding.net/spring-mvc-requestparam-binding-request-parameters/有一个解释:

Examples without @RequestParam 没有@RequestParam的示例

Based on the list of HandlerMethodArgumentResolver configured in your application, @RequestParam can also be omitted. 根据您的应用程序中配置的HandlerMethodArgumentResolver列表,也可以省略@RequestParam。 If you have a look at the code of method getDefaultArgumentResolvers() of RequestMappingHandlerAdapter there is the following piece of code at the end: 如果您看一下RequestMappingHandlerAdapter的getDefaultArgumentResolvers()方法的代码,则最后有以下代码:

// Catch-all resolvers.add(new RequestParamMethodArgumentResolver(getBeanFactory(), true)); //捕获所有resolvers.add(new RequestParamMethodArgumentResolver(getBeanFactory(),true));

resolvers.add(new ServletModelAttributeMethodProcessor(true)); resolvers.add(new ServletModelAttributeMethodProcessor(true));

// Catch-all resolvers.add(new RequestParamMethodArgumentResolver(getBeanFactory(), true)); //捕获所有resolvers.add(new RequestParamMethodArgumentResolver(getBeanFactory(),true));

resolvers.add(new ServletModelAttributeMethodProcessor(true)); resolvers.add(new ServletModelAttributeMethodProcessor(true));

Basically, it's added to the resolvers a RequestParamMethodArgumentResolver with useDefaultResolution set to true. 基本上,它是将useDefaultResolution设置为true的RequestParamMethodArgumentResolver添加到解析器的。 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. 通过查看文档,我们可以看到,这意味着方法参数(即BeanUtils.isSimpleProperty(java.lang.Class)中定义的简单类型)即使未加注释也被视为请求参数。 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 . 当您将HandlerMethodArgumentResolver bean传递给您的解析器时, BeanUtil 检查该参数是原始值还是简单的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 点击服务http:// localhost:8080 / rest?username = mypwd&password = uname

You will be able to see the result given below. 您将能够看到下面给出的结果。

Output: username=pwd password=uname 输出: 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); } 在这里,我为@RequestParam提供了一些示例,希望它们可以为您提供帮助: @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. 单词“ required = true”表示该字段必须根据请求提交。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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