Rest Assured – 发送一个包含 POJO 的数组

huangapple 未分类评论60阅读模式
标题翻译

Rest Assured - send a pojo in an array

问题

以下是翻译好的部分:

我有一个用于创建带有值的 JSON 对象的 Pojo。
这可以成功地创建一个对象,例如:

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 请求的请求体中。

有人能帮帮我吗?

谢谢!

英文翻译

I have a Pojo that creates a json object with values
This creates an object fine e.g.

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

This creates a json object just fine

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

But my api only accepts objects encapsulated as an array e.g.

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

I then want to pass this object in an array into the Body of me restAssured POST request

can anyone help me please?

Thanks!

答案1

得分: 0

这里有几个选项...

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

或者...

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

注意,我假设你的 POJO 将被称为 'Collection' 而不是 'Collections'。

无论你使用什么方式将其转换为 JSON,都应该能够解析上述内容并生成你所期望的形式。

英文翻译

Here's a couple options...

List&lt;Collection&gt; collections = new ArrayList&lt;&gt;();
collections.add(new Collection(&quot;foo&quot;, &quot;bar&quot;));

or...

List&lt;Collection&gt; collections = List.of(new Collection(&quot;foo&quot;, &quot;bar&quot;));

Note, i've assumed your pojo would be called 'Collection' rather than 'Collections'

Whatever you're using to convert it into Json should also parse the above into the form you're expecting.

huangapple
  • 本文由 发表于 2020年1月30日 19:03:51
  • 转载请务必保留本文链接:https://java.coder-hub.com/59984536.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定