Optional char parameter ‘questionNum‘ is present but cannot be translated into a null value

Optional char parameter 'questionNum' 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.

 报错信息如上,错误信息指出,当尝试将一个可选的字符参数questionNum转换为null值时,由于该参数被声明为原始类型(primitive type),所以无法进行转换。原始类型(如intchar等)在Java中不能为null,因为它们代表的是值,而不是对象的引用。

源代码是这样的

//条件分页查询
    @GetMapping("/getPaperByPage")
    public Result<PageBean<Papers>> getPaperByPage(@RequestParam Integer pageNum,
                                                   @RequestParam Integer pageSize,
                                                   @RequestParam(required = false) String competitionName,
                                                   @RequestParam(required = false) char questionNum,
                                                   @RequestParam(required = false) Year dateyear,
                                                   @RequestParam(required = false) String prizeGrade) {

        try {
            PageBean<Papers> papersPageBean = paperService.getPaperByPage(pageNum, pageSize, competitionName, questionNum, dateyear, prizeGrade);
            return Result.success(papersPageBean);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("查询论文失败");
    }

原因就在于 questionNum的类型用的是char,然而@RequestParam(required = false)注解表明这个参数是一个可选参数,也就是可以为null,但是char不可为null

解决办法:将questionNum参数声明为对应的包装类(object wrapper),即Character而不是char。包装类是Java为每个原始类型提供的类,它们可以持有null值。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值