SpringBoot如何接收數(shù)組參數(shù)的方法
1.創(chuàng)建一個表單實體類,將數(shù)組封裝到實體類中(Post提交)
表單類代碼:
@Data
public 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.通過方法內參數(shù)傳遞,注意?。?!SpringBoot方法內接收數(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);
}
})
請求頭:

vue axios前端代碼(注意,數(shù)組需要調用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('查詢失敗')
})
}
注意?。?!請求路徑中的參數(shù)必須跟上圖所示的一樣才能被接收到。
到此這篇關于SpringBoot如何接收數(shù)組參數(shù)的方法的文章就介紹到這了,更多相關SpringBoot接收數(shù)組參數(shù)內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
springmvc實現(xiàn)json交互-requestBody和responseBody
本文主要介紹了springmvc實現(xiàn)json交互-requestBody和responseBody的相關知識。具有很好的參考價值。下面跟著小編一起來看下吧2017-03-03
Java兩種動態(tài)代理JDK動態(tài)代理和CGLIB動態(tài)代理詳解
這篇文章主要介紹了Java兩種動態(tài)代理JDK動態(tài)代理和CGLIB動態(tài)代理詳解,代理模式是23種設計模式的一種,他是指一個對象A通過持有另一個對象B,可以具有B同樣的行為的模式,為了對外開放協(xié)議,B往往實現(xiàn)了一個接口,A也會去實現(xiàn)接口,需要的朋友可以參考下2023-11-11
Java警告:原發(fā)性版11需要目標發(fā)行版11的解決方法和步驟
這篇文章主要介紹了Java警告:原發(fā)性版11需要目標發(fā)行版11的解決方法和步驟,文中通過圖文介紹的非常詳細,對大家學習或者使用java具有一定的參考借鑒價值,需要的朋友可以參考下2025-04-04

