在没有嵌套的情况下使用@Embeddable JSON

Use @Embeddable Without Nested JSON

我正在尝试使用 @Embedded and @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"
    }
]

是否可以使用这些注释?

我目前拥有的:

Organization.java

@Embedded
private Address address;

Address.java

@Embeddable
public class Address {
    ...
}

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

public class Organization {

    @JsonUnwrapped
    @Embedded
    private Address address;
   
    // other code
}