SpringMVC和SpringBoot接收复杂集合参数,集合对象

在SpringMVC和SpringBoot中,我们有这样一个场景,就是接受前端复杂的对象数组,或者多个数组。这是我们可以用@RequestBody注解,来解析请求体中的数据。但是值得注意的是在GET请求是不能进行该种方式的操作,因为GET请求没有请求体。下面就搭建一个简单的demo进行展示:

一、 数据准备

1.1 json数据准备

# 第一段json数据
[
    {
        "id": "xxxx",
        "goodsId": "1001",
        "name": "苹果手机",
        "number": 1
    }, {
        "id": "xxxx",
        "goodsId": "1002",
        "name": "华为手机",
        "number": 2
    }
]

# 第二段json数据
 {
     "orderId": "11xxcqdffvf",
     "order": [
         {
             "id": "xxxx",
             "goodsId": "1001",
             "name": "苹果手机",
             "number": 1
         },
         {
             "id": "xxxx",
             "goodsId": "1002",
             "name": "华为手机",
             "number": 2
         }],
     "stock": [
         {
             "id": "xxxx",
             "goodsId": "1001",
             "name": "苹果手机",
             "number": 1
         },
         {
             "id": "xxxx",
             "goodsId": "1002",
             "name": "华为手机",
             "number": 2
         }]
 }

1.2 编写实体对象

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Product {
    private String id;

    private String goodsId;

    private String name;

    private int number;
}

1.3 搭建一个SpringMVC或者SpringBoot项目,这里搭建一个SpringBoot项目

二、编写测试

2.1 一个对象数组的请求

2.1.1 前端数据请求编写

$.ajax({
    type: "put",
    url: "http://localhost:8080/test/save",
    contentType: 'application/json;charset=utf-8',
    data: JSON.stringify([
        {
            "id": "xxxx",
            "goodsId": "1001",
            "name": "苹果手机",
            "number": 1
        }, {
            "id": "xxxx",
            "goodsId": "1002",
            "name": "华为手机",
            "number": 2
        }
    ]),
    success: function (res) {}
});

2.1.2 编写后端接口

@RestController
@RequestMapping("/test")
public class OrderController {
    // 创建订单
    @PutMapping("/save")
    public Object createOrder(@RequestBody List<Product> productList) {
        return productList;
    }
}
2.2 多个对象数组请求

2.2.1 前端数据请求编写

$.ajax({
    type: "put",
    url: "http://localhost:8080/test/save",
    contentType: 'application/json;charset=utf-8',
    data: JSON.stringify({
        "orderId": "11xxcqdffvf",
        "order": [
            {
                "id": "xxxx",
                "goodsId": "1001",
                "name": "苹果手机",
                "number": 1
            },
            {
                "id": "xxxx",
                "goodsId": "1002",
                "name": "华为手机",
                "number": 2
            }],
        "stock": [
            {
                "id": "xxxx",
                "goodsId": "1001",
                "name": "苹果手机",
                "number": 1
            },
            {
                "id": "xxxx",
                "goodsId": "1002",
                "name": "华为手机",
                "number": 2
            }]
    }),
    success: function (res) {
    }
});

2.2.2 后端编写

@RestController
@RequestMapping("/test")
public class OrderController {
   
    @PutMapping("/save1")
    public Object createOrder1(@RequestBody Map<String, Object> map) {
        return map;
    }
}
2.3 注意事项
  1. 发送的请求类型不能是GET请求,可以POST,PUT,DELETE等请求。
  2. 前端发送数据的时候,请求内容格式必须设置为contentType:'application/json;charset=utf-8',默认是``contentType:‘www-form-urlencoded’`\
  3. 上传的数据必须使用JSON.stringify(..)处理
  4. 后端接受参数前需要加注解@RequestBody,且每一个请求的参数里面最多只能有一个该注解
  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值