PostMan傳@RequestParam修飾的數組
筆者今天被一個問題困擾了一段時間
看如下代碼:
1
2
3
4
5
6
7
8
|
@RestController @RequestMapping ( "/getTest" ) public class GetTestController { @RequestMapping ( "/listRaramTest" ) public List<String> listRaramTest( @RequestParam ( "userIdList" ) List<String> userIdList){ return userIdList; } } |
@RequestParam修飾了一個list
那么用PostMan該如何傳入數組呢?
實際上很簡單
在這里記錄一下:
其實只要按參數名字傳入一個按都好分隔的字符串就好了
param參數數組使用注意點
1、只能為一位數組使用 params 關鍵字
不能為多為數組使用,否則編譯不能通過。
2、不能只依賴 params 關鍵字來重載一個方法
params 關鍵字不構成方法簽名的一部分,例如:
1
2
3
4
5
|
//編譯時錯誤:重復的聲明 public static int Max( int [] paramList) ... public static int Max(params int [] paramList) ... |
3、不允許為 params
數組指定 ref 或 out修飾符。
4、params 數組
必須是方法的最后一個參數,沒個方法中也只能有一個 params 數組參數。
1
2
3
|
//編譯時錯誤 public static int Max(params int [] paramList, int i) ... |
5、非params方法優先
1
2
3
4
|
public static int Max( int first, int second) //優先 ... public static int Max(params int [] paramList) ... |
對于上面的重載方法,傳入兩個 int 參數時,調用上面的方法,傳入其它任意數量的 int 參數時,調用下面的方法。這樣做并非多余,實則起到優化作用。
6、有歧義的重載編譯不能通過
1
2
3
4
5
|
//編譯時錯誤 public static int Max(params int [] paramList) ... public static int Max( int i, params int [] paramList) ... |
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/a907691592/article/details/107051586