使用SpringBoot怎么对数组的参数进行接收

发布时间:2020-12-21 14:41:22 作者:Leah
来源:亿速云 阅读:1633

这篇文章将为大家详细讲解有关使用SpringBoot怎么对数组的参数进行接收,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

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);
  }
  })

请求头: 

使用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怎么对数组的参数进行接收就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. springboot 启动接收参数
  2. SpringBoot中怎么接收请求参数

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springboot 数组

上一篇:利用springboot怎么访问template中的html页面

下一篇:小程序加载app-service.js错误怎么办

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》