問題描述:
好久不寫博客了,今天遇到一個問題,那就是post請求時,參數接收不到,當時我很納悶,看代碼:
就是這樣幾個參數,我使用postman請求時無法獲取參數:
?
報錯信息:
"msg":"Optional int parameter 'tableId' is present but cannot be translated into a null value due to
后來上網查了相關資料:
接口測試的時候遇到了一個問題,導致測試阻斷了好久,在此記錄,謹防忘記。 具體報錯如下: Optional int parameter 'pId' 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. 歸根結底就是參數類型錯誤了: 可選的參數 pId不存在,但無法被轉換為NULL,是因為你把它給定義為 基本類型。建議將其修改為 包裝類型。 就是說,你定義了參數:String pId,但沒有值,那按理來說按照null來處理,結果倒霉的事情來了:pId= null; 是不允許的,因為基礎類型不能賦值為null。 所以建議把參數定義修改為Inteter pId. 那為啥用Integer可以,用int不行呢,原因如下: Integer 允許為null值,int默認0,數據庫里面如果有個字段沒有值可能默認值為null,所以用Integer。 在hashmap中只能用Integer而不能用int int是基本數據類型,定義一個整型數據。Integer是一個類,在hashmap中代表一個對象,所以用object表示。
解決方案:
后來我這么改還是報錯,我想我可能將參數放錯地方了
?