服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - SpringMvc @RequestParam 使用推荐使用包装类型代替包装类型

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

2021-07-16 15:17喜欢日向雏田一样的女子 Java教程

这篇文章主要介绍了SpringMvc @RequestParam 使用推荐使用包装类型代替包装类型,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

SpringMvc 中@RequestParam注解使用

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

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

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

.@RequestParam属性作用

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

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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
2
3
4
5
6
7
8
9
10
11
12
13
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不存在 且 参数类型为基本数据类型;

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

原文链接:http://www.cnblogs.com/lvbinbin2yujie/p/10398652.html

延伸 · 阅读

精彩推荐