首页 > 解决方案 > 放心 - 在数组中发送 pojo

问题描述

我有一个 Pojo,它创建一个带有值的 json 对象这可以很好地创建一个对象,例如

public class Collections {

private String reference;
private String collectionDate;

public String getReference() {
    return reference;
}

public void setReference(String reference) {
    this.reference = reference;
}

public String getCollectionDate() {
    return collectionDate;
}

public void setCollectionDate(String collectionDate) {
    this.collectionDate = collectionDate;
}}

这会创建一个 json 对象就好了

{reference:"test",collectionDate:"test"}

但是我的 api 只接受封装为数组的对象,例如

[{reference:"test",collectionDate:"test"}]

然后我想将这个对象在一个数组中传递给我的主体 restAssured POST 请求

谁能帮帮我?

谢谢!

标签: javarest-assured

解决方案


这里有几个选择...

List<Collection> collections = new ArrayList<>();
collections.add(new Collection("foo", "bar"));

或者...

List<Collection> collections = List.of(new Collection("foo", "bar"));

请注意,我假设您的 pojo 将被称为“收藏”而不是“收藏”

无论您使用什么将其转换为 Json,也应该将上述内容解析为您期望的形式。


推荐阅读