温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringMvc @RequestParam 使用推荐使用包装类型代替包装类型

发布时间:2020-08-30 11:54:55 来源:脚本之家 阅读:186 作者:喜欢日向雏田一样的女子啊 栏目:编程语言

SpringMvc 中@RequestParam注解使用

建议使用包装类型来代替基本数据类型

public String form2(@RequestParam(name="age") int age){
public String form2(@RequestParam(name="age") Integer age) {

上述两种方式 这种情况下使用起来基本没有差别,但是为什么要说建议使用包装类型而不是基本类型呢?

.@RequestParam属性作用

因为当@RequestParam注解 required 属性(默认为true,代表该参数在请求中必不可少) 设置为false时,判断的标准是这样的:

Object arg = resolveName(resolvedName.toString(), nestedParameter, webRequest);
if (arg == null) {
  if (namedValueInfo.defaultValue != null) {
   arg = resolveStringValue(namedValueInfo.defaultValue);
  }
  else if (namedValueInfo.required && !nestedParameter.isOptional()) {
   handleMissingValue(namedValueInfo.name, nestedParameter, webRequest);
  }
  arg = handleNullValue(namedValueInfo.name, arg, nestedParameter.getNestedParameterType());
}
else if ("".equals(arg) && namedValueInfo.defaultValue != null) {
  arg = resolveStringValue(namedValueInfo.defaultValue);
}

上述代码为Spring AbstractNamedValueMethodArgumentResolver 的 resolveArgument 方法,顾名思义就是解析请求中参数并完成类型转换的方法;

arg 是从请求中获取的对应参数值,调用 request.getParameterValues(name) ;

当arg==null时,意味着请求中不包含该参数(即请求中不包含age参数),@RequestParam的defaultValue不为空 那就使用 defaultValue作为请求中的参数,

但是required为true且默认值为null,就会执行handleMissingValue抛出异常,请求中缺少对应参数 ;

两种逻辑都没有执行就代表required为 false 且 默认值为 null ,这时候就会抛出另外一种异常,java.lang.IllegalStateException: Optional int parameter 'age' 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

查看异常说明,age参数存在但是无法转为null类型,因为age被定义为基本数据类型了,建议把它声明为对应的包装类型;

但是八种基本数据类型测试的时候, 就是 布尔类型 boolean,代码原因如下:

可以看到Spring的解析当方法入参为boolean类型时候,直接返回Boolean.FALSE,但是其他七个基本数据类型就抛出异常了;

(补充一句,Spring mvc:annotation-driven使用的情况下,比如请求中传入属性需要赋给布尔值,该属性值为 true 1 on yes这四个都可以赋给boolean类型的)

private Object handleNullValue(String name, Object value, Class<?> paramType) {
    if (value == null) {
      if (Boolean.TYPE.equals(paramType)) {
        return Boolean.FALSE;
      }
      else if (paramType.isPrimitive()) {
        throw new IllegalStateException("Optional " + paramType.getSimpleName() + " parameter '" + name +
            "' 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.");
      }
    }
    return value;
}

二.@RequestParam使用情形列举

简而言之@RequestParam使用如下:

@RequestParam  name必须存在的情况            defaultValue存在              defaultValue不存在
required为true 请求中存在该参数     按照该参数来传递 请求中存在该参数     按照该参数来传递
请求中不存在该参数  使用默认值来传递 请求中不存在该参数  抛出缺少参数异常
required为false 请求中存在该参数     按照该参数来传递 请求中存在该参数     按照该参数来传递
请求中不存在该参数  使用默认值来传递 请求中不存在该参数  使用null来传递

总结就是请求中包含参数信息,就使用请求中的参数;使用默认值的情况除上图两种以外,比如请求中值为空字符串"" 且 defaultValue不为null,那也是用DefaultValue;

三.@RequestParam出现两种异常原因解析

Spring @RequestParam中可能抛出两种异常原因解释:

异常一. Required int parameter 'age' is not present

SpringMvc @RequestParam 使用推荐使用包装类型代替包装类型

异常原因:required为true 且 请求中不包含 对应的参数 ;

异常二.Optional int parameter 'age' is present but cannot be translated into a null value due to being declared as a primitive type.

  SpringMvc @RequestParam 使用推荐使用包装类型代替包装类型

异常原因:required为false 且 defaultValue不存在 且 参数类型为基本数据类型;

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI