您的位置:首页 > 编程语言 > Java开发

使用SpringMVC注解@RequestParam(value="XXX",required=false)时需要注意的问题

2012-11-04 14:00 841 查看
错误描述:

[java]
view plaincopy

@RequestMapping(value = "/index")
public String index(@RequestParam(value = "action", required = false)
String action, @RequestParam(value = "notIncludeTypeId", required = false)
int notIncludeTypeId){
// .... 省略代码
}

当可选参数“notIncludeTypeId”为空时,系统出现如下错误:

[plain]
view plaincopy

Optional int parameter 'notIncludeTypeId' is not present
but cannot be translated into a null value due to being declared as a primitive type.
Consider declaring it as object wrapper for the corresponding primitive type.

错误原因:

可选参数“notIncludeTypeId”不存在时,Spring默认将其赋值为null,但由于notIncludeTypeId已定于为基本类型int,所赋值失败!

解决方法:

“Consider declaring it as object wrapper for the corresponding primitive type.”建议使用包装类型代替基本类型,如使用“Integer”代替“int”
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐