久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

SpringBoot如何接收數(shù)組參數(shù)的方法

瀏覽:138日期:2023-07-28 10:32:28
1.創(chuàng)建一個(gè)表單實(shí)體類,將數(shù)組封裝到實(shí)體類中(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.通過方法內(nèi)參數(shù)傳遞,注意!!!SpringBoot方法內(nèi)接收數(shù)組時(shí),數(shù)組在前端請求時(shí)必須將參數(shù)拼接在路徑里提交才可以接收到。(Get提交)

后端代碼:

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

小程序前端代碼:參數(shù)需拼接到路徑里,并且要以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如何接收數(shù)組參數(shù)的方法

vue axios前端代碼(注意,數(shù)組需要調(diào)用encodeURIComponent進(jìn)行編碼):

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(’查詢失敗’) }) }

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

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

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 阿勒泰市| 咸宁市| 尤溪县| 深圳市| 宁明县| 上杭县| 桃江县| 固阳县| 奎屯市| 宜兴市| 南充市| 新宾| 汉川市| 衡水市| 湄潭县| 祁东县| 衡东县| 绥江县| 西丰县| 太原市| 南投县| 本溪| 嘉善县| 山西省| 宿松县| 郸城县| 正阳县| 咸阳市| 邹城市| 湘阴县| 四会市| 翼城县| 沙坪坝区| 嘉鱼县| 长阳| 滦南县| 石家庄市| 手游| 湟中县| 安西县| 盐城市|