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不存在 且 引數型別為基本資料型別;