标题翻译
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<Collection> collections = new ArrayList<>();
collections.add(new Collection("foo", "bar"));
or...
List<Collection> collections = List.of(new Collection("foo", "bar"));
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.
专注分享java语言的经验与见解,让所有开发者获益!
评论