gpt4 book ai didi

java - 有没有办法从具有对象组合的 pojo 自动生成展平 pojo

转载 作者:行者123 更新时间:2023-11-30 05:48:15 26 4
gpt4 key购买 nike

我需要将文档存储到 Elasticsearch 索引中,因此我定义了一个映射。我需要从我的 java 客户端向它提供看起来像 Compony 类的 pojo。它有很多重复的值。我可以使用对象组合模式来解决这个问题,但 Elasticsearch 无法处理这种结构,因此它需要扁平结构。

class Company {
private String postalstreetName;
private String postalHouseNumer;
private String postalHouseLetter;
private String postalHouseNumberAddition;
private String postalZipCode;
private String postalCity;
private String postalCountry;

private String visitorstreetName;
private String visitorHouseNumer;
private String visitorHouseLetter;
private String visitorHouseNumberAddition;
private String visitorZipCode;
private String visitorCity;
private String visitorCountry;

private String establishmentstreetName;
private String establishmentHouseNumer;
private String establishmentHouseLetter;
private String establishmentHouseNumberAddition;
private String establishmentZipCode;
private String establishmentCity;
private String establishmentCountry;
}

我实际上想要像下面这样的 pojo,并以某种方式自动生成上面具有相同命名结构的 pojo。

class Address {
private String streetName;
private String houseNumer;
private String houseLetter;
private String houseNumberAddition;
private String zipCode;
private String city;
private String country;
}
class Company {
private Address postalAddress;
private Address visitorAddress;
private Address establishmentAddress;
}

有人知道这样的事情是否可能,从具有对象组合的 pojo 自动生成一个扁平化的 pojo,并为所有字段添加名称前缀?

最佳答案

我的问题得到了解决。 Elasticsearch 需要一个 json 对象,其结构与第一个示例中的结构相同。所以我所做的是将 @JsonUnwrapped 添加到带有前缀值的字段中,所以我的 Company 类如下所示:

class Company {
@JsonUnwrapped(prefix = "postal")
private Address postalAddress;
@JsonUnwrapped(prefix = "visitor")
private Address visitorAddress;
@JsonUnwrapped(prefix = "Establishment")
private Address establishmentAddress;
}

结果将是:

{
"postalStreetName":"",
"postalHouseNumer":"",
"postalHouseLetter":"",
"postalHouseNumberAddition":"",
"postalCity":"",
"postalCountry":"",
"visitorStreetName":"",
"visitorHouseNumer":"",
"visitorHouseLetter":"",
"visitorHouseNumberAddition":"",
"visitorCity":"",
"visitorCountry":"",
"establishmentStreetName":"",
"establishmentHouseNumer":"",
"establishmentHouseLetter":"",
"establishmentHouseNumberAddition":"",
"establishmentCity":"",
"establishmentCountry":""
}

所以我不需要以某种方式神奇地创建这些 pojo,因为 jackson 可以创建我需要的 json 文件。

关于java - 有没有办法从具有对象组合的 pojo 自动生成展平 pojo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54458592/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com