错误描述:[java] view plain copy@RequestMapping(value = "/index") public String index(@RequestParam(value = "action", required = false) String action, @RequestParam(value = "notIncludeTypeId", required = false) int notIncludeTypeId){ // .... 省略代码 } 当可选参数“notIncludeTypeId”为空时,系统出现如下错误:
[plain] view plain copyOptional 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”
使用SpringMVC注解@RequestParam(value="XXX",required=false)时需要注意的问题
最新推荐文章于 2024-09-08 09:57:49 发布
本文探讨了在Spring MVC框架中处理可选请求参数时遇到的问题。特别是当使用基本类型int接收可选参数且该参数未传递时,系统将抛出错误。文章详细解释了错误的原因,并提供了解决方案:通过使用Integer类型替代int类型来避免此类问题。
1万+

被折叠的 条评论
为什么被折叠?



