gpt4 book ai didi

java - @RequestParam 省略但映射仍然正确完成

转载 作者:行者123 更新时间:2023-12-01 14:21:33 27 4
gpt4 key购买 nike

我刚刚发现,即使我省略了 @RequestParam organization 上的注释参数,Spring仍然能够绑定(bind)它。

@RequestMapping(value="", 方法 = RequestMethod.POST)
@ResponseBody
公共(public)字符串保存(字符串组织){
logger.info(组织);//有用
}

谁能指出澄清这种行为的文档?我一直认为 @RequestParam对于绑定(bind)工作是强制性的。

谢谢

最佳答案

看看https://reversecoding.net/spring-mvc-requestparam-binding-request-parameters/有一个解释:

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.

关于java - @RequestParam 省略但映射仍然正确完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47967040/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com