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

這篇文章主要介紹了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不存在 且 參數類型爲基本數據類型;

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持神馬文庫。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章