SpringMvc @RequestParam 使用推薦使用包裝類型代替包裝類型


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

      異常原因: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不存在  且 參數類型為基本數據類型;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM