java控制器与接口_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 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 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!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值