SpringMVC 获取客户端数组列表

最新需要用到SpringMVC获取浏览器传来的数组列表,百度了几个方法,结果都失败了,因此只能用最笨的方法,穷举得到答案。

编程语言:Kotlin

框架:SpringBoot+SpringMVC

本次测试的内容有List<Int>和Array<Int>。

List<Int>同Java的List<Integer>。Array<Int>同Java的Integer[]

fun fun1(id: Array<Int>)//只能通过id=1&id=2
fun fun11(id: List<Int>)//id=1&id=2和id[]=1&id[]=2和id[0]=1&id[1]=2均无法通过

fun fun2(@RequestBody id: Array<Int>)//id=1&id=2和id[]=1&id[]=2和id[0]=1&id[1]=2均无法通过
fun fun22(@RequestBody id: List<Int>)//id=1&id=2和id[]=1&id[]=2和id[0]=1&id[1]=2均无法通过

fun fun3(@RequestParam(name = "id") id: Array<Int>)//只能通过id=1&id=2
fun fun33(@RequestParam(name = "id") id: List<Int>)//只能通过id=1&id=2

fun fun4(@RequestParam(name = "id[]") id: Array<Int>)//只能通过id[]=1&id[]=2
fun fun44(@RequestParam(name = "id[]") id: List<Int>)//只能通过id[]=1&id[]=2

fun fun5(@RequestParam(value = "id") id: Array<Int>)//只能通过id=1&id=2
fun fun55(@RequestParam(value = "id") id: List<Int>)//只能通过id=1&id=2

fun fun6(@RequestParam(value = "id[]") id: Array<Int>)//只能通过id[]=1&id[]=2
fun fun66(@RequestParam(value = "id[]") id: List<Int>)//只能通过id[]=1&id[]=2

fun model1(model: Model1)//只有id=1&id=2和id[0]=1&id[1]=2能通过
fun model11(model: Model11)//只有id=1&id=2和id[0]=1&id[1]=2能通过

fun model2(model: Model2)//只有bean[0].id=1&bean[1].id=2能通过,bean.id=1&bean.id=2会返回空数组
fun model22(model: Model22)//只有bean[0].id=1&bean[1].id=2能通过,bean.id=1&bean.id=2会返回空数组

class Model1 {
    var id: Array<Int>? = null
}
class Model11 {
    var id: List<Int>? = null
}
class Model2 {
    var bean: Array<Bean>? = null
}
class Model22 {
    var bean: List<Bean>? = null
}

鉴于以上测试结果,如果从浏览器传入的数组数据带有下标的话,也就是id[0]=1&id[1]=2则只能通过后两种方法接收数组,创建一个class类来存放数组内容。

创建一个类来接收数组的话,它可以接收两种类型的数组,一种是带有下标,一种是不带下标但是键名相同。

如果浏览器传入的数组是不带下标但是带有方括号的(id[]=1&id[]=2),则可以通过以下两种方法获取数组比较方便:

fun fun4(@RequestParam(name = "id[]") id: Array<Int>)//只能通过id[]=1&id[]=2
fun fun44(@RequestParam(name = "id[]") id: List<Int>)//只能通过id[]=1&id[]=2

fun fun6(@RequestParam(value = "id[]") id: Array<Int>)//只能通过id[]=1&id[]=2
fun fun66(@RequestParam(value = "id[]") id: List<Int>)//只能通过id[]=1&id[]=2

一般浏览器传参数不会出现传数组时不带方括号只重复键名(id=1&id=2)的这种情况,所以对于以下方法可以不考虑:

fun fun1(id: Array<Int>)//只能通过id=1&id=2

fun fun3(@RequestParam(name = "id") id: Array<Int>)//只能通过id=1&id=2
fun fun33(@RequestParam(name = "id") id: List<Int>)//只能通过id=1&id=2

fun fun5(@RequestParam(value = "id") id: Array<Int>)//只能通过id=1&id=2
fun fun55(@RequestParam(value = "id") id: List<Int>)//只能通过id=1&id=2

 

智商不够,脑子笨,只能采用这种方法来得到答案,在这里记录一下,同时也能够给需要的人提供这么一份测试结果,可以拿来直接用。

转载于:https://my.oschina.net/houkunlin/blog/1554421

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值