java – Spring @RequestParam和控制器接口

我对@RequestParam(value =“someValue”)的行为感到困惑.

在 docs据说

When using controller interfaces (e.g. 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.

如果我将@RequestParam放在我的控制器接口上,它的值将被完全忽略(因此,如果参数名称与接收的参数名称不同,则映射值为null),但defaultValue和所需的工作正常.

如果我将@RequestParam放在我的控制器实现上,一切正常.

我阅读了this的回答,但我无法理解为什么有些参数可以正常工作而有些参数却没有,为什么文档错误.

代码示例:

接口:

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

执行:

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

最佳答案 这最终应该解决: https://jira.spring.io/browse/SPR-11055?focusedCommentId=160889&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-160889

Juergen Hoeller added a comment – Yesterday

I’m happy to report that this is finally resolved in master now, in time for the 5.1 RC1 release!

点赞