其他分享
首页 > 其他分享> > int parameter ‘‘ is present but cannot be translated into a null,Integer parameter ‘‘is not present

int parameter ‘‘ is present but cannot be translated into a null,Integer parameter ‘‘is not present

作者:互联网

相关异常

  1. java.lang.IllegalStateException: 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.
  2. org.springframework.web.bind.MissingServletRequestParameterException:Required Integer[] parameter ‘chk_no_value[]’ is not present

错误例子

@ResponseBody
@RequestMapping(value = "/add_Credititem_audit")//给数组注解时,格式:@RequestParam("数组名[]"),[]中括号切记不能少!
public Object add_Credititem_audit(HttpSession httpSession,
                         int sid,
                         @RequestParam(value = "chk_yes_value[]") Integer[] chk_yes_value,
                         @RequestParam(value = "chk_no_value[]") Integer[] chk_no_value,
                         @RequestParam(value = "auditopinion_value[]") String[] auditopinion_value){

        //@PathVariable("position_id") Integer position_id,
        System.out.println("add_Credititem_audit:");
        Map<String,Object> maps = new HashMap<>();
        int position_id = Integer.parseInt((httpSession.getAttribute("position_id")).toString());

            audit_Servlet.add_Credititem_audit(position_id, sid, chk_yes_value, chk_no_value, auditopinion_value);
            maps.put("count",1);

        return maps;
    }

一、第一个异常

  1. 分析问题
    翻译:可选的int参数“id”存在,但由于被声明为基本类型,因此无法转换为null值。考虑将其声明为对应的基本类型的对象包装器。
  2. 解决方法
    将形参的int换成Integer

二、第二个异常

  1. 分析问题
    @RequestParam 还有一个 required 属性,来要求 @RequestParam 配置的前端参数是否一定要传。不写时默认required=true,就是必须要有,当参数为空时就会报这个MissingServletRequestParameterException异常

  2. 解决方法
    可以通过设置 required=false ,表示不传,它会给参数赋值为 null。

特别注意:
如果注解的参数是 int 基本类型,这时再用 required=false,就会报错,因为不传值,把 null 赋值给 int,肯定会报错,报第一个异常,要解决这个错误同样是把基本数据类型改为包装数据类型即可(int 改为 Integer)。
由于我的业务原因可以保证sid是有值的,所以这里使用int,且使用前也不加以空值判断不会报错。
但是,我在开发阶段由于前面js代码出错(相关https://blog.csdn.net/weixin_46109017/article/details/119383106)导致sid为空报第一个异常,,笨拙的我被迷惑了,死磕空指针异常搜了很多关于这个异常的,当我改好js后空指针异常也就不复存在,靠!,不过还是建议大家使用Integer,在用之前判断是否为空(相关https://blog.csdn.net/weixin_46109017/article/details/119417443

上面修改后的成功例子

@ResponseBody
@RequestMapping(value = "/add_Credititem_audit")//给数组注解时,格式:@RequestParam("数组名[]"),[]中括号切记不能少!
public Object add_Credititem_audit(HttpSession httpSession,
                    Integer sid,
                    @RequestParam(value = "chk_yes_value[]", required = false) Integer[] chk_yes_value,
                    @RequestParam(value = "chk_no_value[]", required = false) Integer[] chk_no_value,
                    @RequestParam(value = "auditopinion_value[]", required = false) String[] auditopinion_value){

        //@PathVariable("position_id") Integer position_id,
        System.out.println("add_Credititem_audit:");
        Map<String,Object> maps = new HashMap<>();
        int position_id = Integer.parseInt((httpSession.getAttribute("position_id")).toString());
        if (sid!=null) {
            audit_Servlet.add_Credititem_audit(position_id, sid, chk_yes_value, chk_no_value, auditopinion_value);
            maps.put("count", 1);
        }
        return maps;
    }

标签:RequestParam,int,chk,value,Integer,parameter,id,present
来源: https://blog.csdn.net/weixin_46109017/article/details/119421731