當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringMVC接受List的几种方式
生活随笔
收集整理的這篇文章主要介紹了
SpringMVC接受List的几种方式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
說到SpringMVC接受List參數,可能有人會說這個是個坑!下面我來跟大家說怎么補這個坑。下面我分幾種情況來講:
一、只接收一個List
1、form表單提交
controller代碼:
@ResponseBody @RequestMapping("test") public void test(@RequestParam(value="list",required = false) List<Integer> list) {for (Integer integer : list) {System.out.println(integer);}}postman:
2、提交json對象
controller代碼:
@ResponseBody @RequestMapping("test")public void test(@RequestBody List<Integer> list) {for (Integer integer : list) {System.out.println(integer);}}postman:
請求頭設置:Content-Type application/json
總結一下:
controller 的區別在接收參數注解不一樣,form表單提交是@RequestParam,接收json對象是@RequestBody
二、同時接收List和String,Interger等其他參數
1、form表單提交
controller代碼:
@ResponseBody@RequestMapping("test")public void test(@RequestParam(value="list",required = false) List<Integer> list,@RequestParam(value="test",required = false) String test) {for (Integer integer : list) {System.out.println(integer);}System.out.println(test);}postman:
2、接收json對象
controller代碼:
@ResponseBody@RequestMapping("test")public void test(@RequestBody TestList testList) {for (Integer integer : testList.getList()) {System.out.println(integer);}System.out.println(testList.getTest());}TestList 是自己寫的接收對象:
public class TestList {private List<Integer> list;private String test;public List<Integer> getList() {return list;}public void setList(List<Integer> list) {this.list = list;}public String getTest() {return test;}public void setTest(String test) {this.test = test;} }postman:
請求Headers和之前一樣
三、同時接收List、Map、String/Interger參數
controller代碼:
@ResponseBody @RequestMapping("test") public void test(@RequestBody TestList testList) {for (Integer integer : testList.getList()) {System.out.println(integer);}System.out.println(testList.getTest());System.out.println(testList.getMap()); }pojo對象:
public class TestList {private List<Integer> list;private String test;private Map<String, String> map;public Map<String, String> getMap() {return map;}public void setMap(Map<String, String> map) {this.map = map;}public List<Integer> getList() {return list;}public void setList(List<Integer> list) {this.list = list;}public String getTest() {return test;}public void setTest(String test) {this.test = test;} }postman:
接收成功!
注:POJO的傳遞和Map是一樣的,把“map”改成你的對象名稱,“ggg”這些換成對象的成員名稱就可以了
四、接收List
controller:
@ResponseBody@RequestMapping("test")public void test(@RequestBody List<PojoList> list) {for (PojoList pojoList : list) {System.out.println(pojoList.getTest1());System.out.println(pojoList.getTest2());}}pojo:
public class PojoList {private String test1;private Integer test2;public String getTest1() {return test1;}public void setTest1(String test1) {this.test1 = test1;}public Integer getTest2() {return test2;}public void setTest2(Integer test2) {this.test2 = test2;} }postman:
總結一下:如果是單個List或者List帶一些其他簡單參數,form表單提交和Json對象提交都差不多,但是如果是比較復雜的數據,組合,個人建議建一個pojo去組合這些參數,然后再去接收。希望能幫到大家,如果有哪里不正確,希望指正,謝謝!!!
總結
以上是生活随笔為你收集整理的SpringMVC接受List的几种方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 工业用微型计算机(28)-dos和bio
- 下一篇: PyTorch随笔-1