0

我正在尝试使用@Embedded 和 @Embeddable javax 注释来保持我的 Java 类更干净,但我希望生成的 JSON 被展平。

期望的行为:

[
    {
        "id": "6edbced5-2d27-4257-a140-2925291daaf6",
        "name": "Online Maria DB",
        "address": "Syble Forks",
        "city": "Dallas",
        "state": "Texas",
        "country": "United States"
        "phoneNumber": "(789) 740-5789",
        "orgUserName": "online-maria"
    }
]

实际行为:

[
    {
        "id": "6edbced5-2d27-4257-a140-2925291daaf6",
        "name": "Online Maria DB",
        "addressDetails": {
            "address": "Syble Forks",
            "city": "Dallas",
            "state": "Texas",
            "country": "United States"
        },
        "phoneNumber": "(789) 740-5789",
        "orgUserName": "online-maria"
    }
]

这可以使用这些注释吗?

到目前为止我所拥有的:

组织.java

@Embedded
private Address address;

地址.java

@Embeddable
public class Address {
    ...
}

4

1 回答 1

1

如果你确实使用了,你可以使用 Jackson 的@JsonUnwrapped注释Jackson。您也可以编写自定义序列化程序

public class Organization {

    @JsonUnwrapped
    @Embedded
    private Address address;
   
    // other code
}
于 2021-10-02T17:09:30.863 回答