简体   繁体   English

Spring @RequestParam和控制器接口

[英]Spring @RequestParam and controller interface

I'm confused with the behavior of @RequestParam(value="someValue") . 我对@RequestParam(value="someValue")的行为感到困惑。 In the docs it is said that 据说,在文档

When using controller interfaces (eg for AOP proxying), make sure to consistently put all your mapping annotations - such as @RequestMapping and @SessionAttributes - on the controller interface rather than on the implementation class. 使用控制器接口(例如,用于AOP代理)时,请确保在控制器接口而不是实现类上始终如一地放置所有映射注释(例如@RequestMapping和@SessionAttributes)。

If I place @RequestParam on my controller interface its value is totally ignored (and therefore mapped value is null if parameter name is different from the received parameter name), but defaultValue and required work ok. 如果我将@RequestParam放在我的控制器接口上,它的value被完全忽略(因此,如果参数名称与接收的参数名称不同,则映射值为null ),但defaultValuerequired工作正常。

If I place @RequestParam on my controller implementation, everything works fine. 如果我将@RequestParam放在我的控制器实现上,一切正常。

I read this answer, but I cannot understand why some parameters work while others don't and why docs are wrong. 我读了这个答案,但是我无法理解为什么有些参数可以正常工作而其他参数却没有,为什么文档错了。

Code example: 代码示例:

interface: 接口:

@RequestMapping(method = RequestMethod.GET)
List<MyObject> get(
     //works if parameter in request has name "userName", which is not correct
     @RequestParam(value = "username", required = false) String userName,
     @RequestParam(value = "searchValue", required = false) String searchValue,
     @RequestParam(value = "someId", required = false) Integer someId);

implementation: 执行:

@Override
public List<MyObject> get(
        String userName,
        String searchValue,
        Integer someId) {
    return myService.get(userName, searchValue, someId);
}

This finally should be resolved: https://jira.spring.io/browse/SPR-11055?focusedCommentId=160889&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-160889 这最终应该得到解决: https//jira.spring.io/browse/SPR-11055?focusCommentId = 160889&page = com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-160889

Juergen Hoeller added a comment - Yesterday Juergen Hoeller添加了评论 - 昨天

I'm happy to report that this is finally resolved in master now, in time for the 5.1 RC1 release! 我很高兴地报告,现在终于在master中解决了这个问题,及时发布了5.1 RC1!

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

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