我在我的应用程序中定义了这两个实体:
@Entity
public class Avail extends PanacheEntity {
public Integer total;
public Integer limit;
@JsonProperty("last_transactions")
@OneToMany(
mappedBy = "avail",
cascade = CascadeType.ALL,
orphanRemoval = true
)
public <List<Transactions> transactions = new LinkedList<>();
@Entity
public class Transactions extends PanacheEntityBase {
public Integer value;
public String type;
public String description;
@JsonProperty("datetime")
public String ts;
@JsonIgnore
@ManyToOne(fetch = FetchType.LAZY)
public Avail avail;
此 DTO 以 application/json 形式提供响应:
@JsonTypeInfo(use=Id.NAME, include=As.WRAPPER_OBJECT)
@JsonTypeName("avail")
@JsonPropertyOrder({ "total", "dataExtrato", "limit" })
public final class AvailDTO {
public final Integer total;
@JsonProperty("data_extrato")
public final String dataExtrato;
public final Integer limit;
@JsonProperty("last_transactions")
public final List<Transactions> transactions;
这些类生成此响应:
{
"avail": {
"total": 29000,
"data_extrato": "2024-02-24T11:50:15.294900Z",
"limit": 100000,
"last_transactions": \[{
"value": 21000,
"type": "d",
"description": "withdraw",
"datetime": "2024-02-24T14:46:00.904000300Z"
}, {
"value": 50000,
"type": "c",
"description": "deposit",
"datetime": "2024-02-24T14:46:17.837816Z"
}\]
}
}
但是我需要的响应看起来像这样(“last_transactions”位于“avail”之外):
{
"avail": {
"total": 29000,
"data_extrato": "2024-02-24T11:50:15.294900Z",
"limit": 100000,
},
"last_transactions": \[{
"value": 21000,
"type": "d",
"description": "withdraw",
"datetime": "2024-02-24T14:46:00.904000300Z"
}, {
"value": 50000,
"type": "c",
"description": "deposit",
"datetime": "2024-02-24T14:46:17.837816Z"
}\]
}
无法弄清楚如何正确编码我的 DTO 来更改 JSON 结构。
我设法创建了另一个具有 2 个属性的 DTO,它将 AvailDTO 包装在一个属性上,并在另一个属性上设置事务列表。可能不是最优雅的解决方案,但它确实有效。 代码可以在https://github.com/marciocg/rinhaquarkus/tree/main/src/main/java/io/github/marciocg/clientes
查看