时间:2021-05-18 09:42:35 | 栏目:JAVA代码 | 点击:次
表单类代码:
@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);
}
})
后端代码:
@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('查询失败')
})
}
注意!!!请求路径中的参数必须跟上图所示的一样才能被接收到。