接收参数时遇到的问题
开始的代码
@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,业务层好使用。