SpringMvc 中@RequestParam注解使用 

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

   public String form2(@RequestParam(name=“age”) int age){

   public String form2(@RequestParam(name=“age”) Integer age) {

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

 

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

  1. 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类型的)

  1. private Object handleNullValue(String name, Object value, Class<?> paramType) {
  2. if (value == null) {
  3. if (Boolean.TYPE.equals(paramType)) {
  4. return Boolean.FALSE;
  5. }
  6. else if (paramType.isPrimitive()) {
  7. throw new IllegalStateException("Optional " + paramType.getSimpleName() + " parameter '" + name +
  8. "' is present but cannot be translated into a null value due to being declared as a " +
  9. "primitive type. Consider declaring it as object wrapper for the corresponding primitive type.");
  10. }
  11. }
  12. return value;
    }

 

简而言之@RequestParam使用如下:

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

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

     

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

  异常一.  Required int parameter ‘age’ is not present

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

 

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

  

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

版权声明:本文为lvbinbin2yujie原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/lvbinbin2yujie/p/10398652.html