1.创建一个表单实体类,将数组封装到实体类中(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.通过方法内参数传递,注意!!!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);
}
})
请求头:
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接收数组参数内容请搜索萬仟网以前的文章或继续浏览下面的相关文章希望大家以后多多支持萬仟网!
如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!