国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

SpringBoot如何接收數組參數的方法

瀏覽:177日期:2023-07-28 10:32:28
1.創建一個表單實體類,將數組封裝到實體類中(Post提交)

表單類代碼:

@Datapublic class MyForm { private int[] ids;}

控制器代碼:

@Slf4j@RestController@RequestMapping('/info')public class InfoController { @PostMapping('/test') public String test(@RequestBody MyForm form){ log.info(Arrays.toString(form.getIds())); return 'success'; }}

前端代碼:

wx.request({ url:’http://localhost:8085/info/test’, data:{ ids:[1,2,3] }, method:’POST’, success:function(res){ console.log(res); } })2.通過方法內參數傳遞,注意!!!SpringBoot方法內接收數組時,數組在前端請求時必須將參數拼接在路徑里提交才可以接收到。(Get提交)

后端代碼:

@Slf4j@RestController@RequestMapping('/info')public class InfoController { @GetMapping('/test') public String test(int[] ids){ log.info(Arrays.toString(ids)); return 'success'; } }

小程序前端代碼:參數需拼接到路徑里,并且要以GET方式提交

var ids = [1, 2, 3, 4] wx.request({ url: ’http://localhost:8085/info/test?ids=’+ids, method: ’GET’, success: function(res){ console.log(res); } })

請求頭:

SpringBoot如何接收數組參數的方法

vue axios前端代碼(注意,數組需要調用encodeURIComponent進行編碼):

test() { let ary = [1,2,3] let params = { ids:encodeURIComponent(ary),}; that.$http.get('http://localhost:8085/info/test',{params}).then(res=>{ if(res.code==0){ that.$message.success(’查詢成功’) }else { that.$message.error(res.message||’查詢失敗’) } }).catch(error=>{ that.$message.error(’查詢失敗’) }) }

注意!!!請求路徑中的參數必須跟上圖所示的一樣才能被接收到。

到此這篇關于SpringBoot如何接收數組參數的方法的文章就介紹到這了,更多相關SpringBoot接收數組參數內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: 观塘区| 理塘县| 武鸣县| 宜兰市| 竹北市| 陇西县| 唐河县| 邹城市| 宁明县| 新源县| 井陉县| 金山区| 莱阳市| 内江市| 普兰店市| 尚志市| 青州市| 米易县| 永靖县| 扶风县| 沙田区| 文成县| 额济纳旗| 囊谦县| 姚安县| 肇州县| 新泰市| 潢川县| 临泽县| 嫩江县| 昌黎县| 宁海县| 南汇区| 凤台县| 工布江达县| 西畴县| 融水| 武宁县| 随州市| 靖宇县| 文昌市|