SpringBoot实现:前端给后端传送数组

嗨你好,我是陌陌

引入

在实现springboot项目的时候,在postman上模拟json数组,进行传参,但是传参一直失败,如下

第一次尝试

第一次我传递的json格式的数组👇

[
    {
        "goodsId": "1",
        "price": 1000.0,
        "count": 5,
        "userId": "1"
    },
    {
        "goodsId": "3",
        "price": 20,
        "count": 1,
        "userId": "1"
    },
    {
        "goodsId": "1613474338864979970",
        "price": 1500,
        "count": 1,
        "userId": "1"
    }
]

我的后端代码

@PostMapping("/test")
    public Boolean test(List<CartInfo> cartInfos){
        System.out.println(cartInfos);
        return false;
    }

后端报错(也就是说参数映射不上去)

java.lang.IllegalStateException: No primary or single unique constructor found for interface java.util.List

第二次尝试

json数据依然和上方一致

我的后端代码

@PostMapping("/test")
    public Boolean test(@RequestBody List<CartInfo> cartInfos){
        System.out.println(cartInfos);
        return false;
    }

妈呀呀!翻车了,我第一次自己写的时候,这个方法是会报错的呀!但是现在又成了,我去~

💡@RequestBody

@RequestBody主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的);而最常用的使用请求体传参的无疑是POST请求了,所以使用@RequestBody接收数据时,一般都用POST方式进行提交。在后端的同一个接收方法里,@RequestBody与@RequestParam()可以同时使用,@RequestBody最多只能有一个,而@RequestParam()可以有多个。

返回值

[
CartInfo{id='null', goodsId='1', price=1000.0, count=5, userId='1', user=null, goods=null}, 
CartInfo{id='null', goodsId='3', price=20.0, count=1, userId='1', user=null, goods=null}, 
CartInfo{id='null', goodsId='1613474338864979970', price=1500.0, count=1, userId='1', user=null, goods=null}]

第三次尝试

json数据

{
"cart":[
    {
        "goodsId": "1",
        "price": 1000.0,
        "count": 5,
        "userId": "1"
    },
    {
        "goodsId": "3",
        "price": 20,
        "count": 1,
        "userId": "1"
    },
    {
        "goodsId": "1613474338864979970",
        "price": 1500,
        "count": 1,
        "userId": "1"
    }
]
}

我的后端代码

@PostMapping("/test")
    public Boolean test(@RequestBody Cart cart){
        System.out.println(cart);
        List<CartInfo> list = new ArrayList<>(cart.getCart());
        return  orderService.addOrders(list);
    }

后端代码的参数是Cart,是我新建立的一个类,专门用于实现映射

import java.util.List;

@Data
public class Cart {
    private List<CartInfo> cart;
}

返回数据

Cart(
cart=[
	CartInfo{id='null', goodsId='1', price=1000.0, count=5, userId='1', user=null, goods=null},
	CartInfo{id='null', goodsId='3', price=20.0, count=1, userId='1', user=null, goods=null}, 
	CartInfo{id='null', goodsId='1613474338864979970', price=1500.0, count=1, userId='1', user=null, goods=null}
]
)

小的技巧

在做课设的时候,还发现一个小的技巧
一般我们进行插入操作的时候,id值都是自增的,但是有些业务逻辑要求我们插入之后得到刚插入的id值。则下

@Override
    @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
    int insert(Order entity);

但是但是但是,突然发现就算不写@Options中的内容,它也会自动加载,what?一脸懵逼

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值