Optional int parameter ‘id‘ is present but cannot be translated into a null value

接收参数时遇到的问题

开始的代码

@RequestMapping("/deleteComment")
    @ResponseBody
    public ResultInfo deleteComment(@RequestParam(value = "comment_id",required = false) int comment_id,
                                    @RequestParam(value = "id",required = false) int id) {
        return commentService.deleteComment(comment_id,id);
    }

错误如下
Optional int parameter ‘id’ is 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.
这段文字的意思如下
可选的int参数“id”是存在的,但是由于声明为原始类型,所以不能被转换成空值。考虑将其声明为对应原语类型的对象包装器
就是你定义的某个方法的参数,但是这个参数是非必须的,系统会自动给这个字段赋值为null,但是你的字段类型为int,int不能接收null值,所以会报如上的错误。解决办法就是使用包装类 Integer。

修改后的代码

@RequestMapping("/deleteComment")
    @ResponseBody
    public ResultInfo deleteComment(@RequestParam(value = "comment_id",required = false,defaultValue = "0") Integer comment_id,
                                    @RequestParam(value = "id",required = false,defaultValue = "0") Integer id) {
        return commentService.deleteComment(comment_id,id);
    }

将int 改为Integer,并给了默认值为0,业务层好使用。

  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值