java - 放心 - 在数组中发送 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 请求
谁能帮帮我?
谢谢!
解决方案
这里有几个选择...
List<Collection> collections = new ArrayList<>();
collections.add(new Collection("foo", "bar"));
或者...
List<Collection> collections = List.of(new Collection("foo", "bar"));
请注意,我假设您的 pojo 将被称为“收藏”而不是“收藏”
无论您使用什么将其转换为 Json,也应该将上述内容解析为您期望的形式。
推荐阅读
- javascript - 使用对话流通过谷歌助手聊天机器人输入图像
- html - 我有一行 X px 宽的文本。背景颜色也是 X px 宽。如何扩展背景颜色使其比文本更长?
- scikit-learn - 实施修改后的 bagging 的最佳方法是什么?
- android - 添加当前日期到时间 UTC 字符串并转换为设备时间
- django - 收集静态引用到文件夹的错误路径
- javascript - 用 javascript 修改 CSS
- aws-cdk - 用于 Elasticache Redis 集群的 aws cdk
- salesforce - 将自定义链接转到公式字段
- python - 如何正确使用 while 和 if 语句
- asp.net-mvc - 为什么 Id 列没有出现在 MVC5 的 CSHTML 视图中