[英]Orika Mapping nested multi-occurrence elements
我想將所有人(男人和女人)映射到與Orika相同的PersonDto。
class Name {
private String first;
private String last;
private String fullName;
// getters/setters
}
class Womens{
private List<Name> names;
// getters/setters
}
class Mens{
private List<Name> names;
// getters/setters
}
class Person {
private Mens mens;
private Womens womens;
// getters/setters
}
class PersonDto {
private List<Info> info;
// getters/setters omitted
}
class Info {
private String notes;
// getters/setters omitted
}
如果我只測試男士,可以嗎?
mapperFactory.classMap(Person.class, PersonDto.class)
.field("mens.names{first}", "info[0].notes")
.field("mens.names{last}", "info[1].notes")
.field("mens.names{fullName}", "info[2].notes")
.register();
如果我只和女子一起測試,可以嗎?
mapperFactory.classMap(Person.class, PersonDto.class)
.field("womens.names{first}", "info[0].notes")
.field("womens.names{last}", "info[1].notes")
.field("womens.names{fullName}", "info[2].notes")
.register();
但是,如果我測試男女裝,那是KO。 信息數組的大小不正確
mapperFactory.classMap(Person.class, PersonDto.class)
.field("mens.names{first}", "info[0].notes")
.field("mens.names{last}", "info[1].notes")
.field("mens.names{fullName}", "info[2].notes")
.field("womens.names{first}", "info[3].notes")
.field("womens.names{last}", "info[4].notes")
.field("womens.names{fullName}", "info[5].notes")
.register();
我找到了解決方案,但這不是最好的。 如果您有更好的回應,請在此處發布您的解決方案。
我分為兩個Infomaton類:
@XmlRootElement(name = "notes")
class InfoMen {
private String notes;
// getters/setters omitted
}
@XmlRootElement(name = "notes")
class InfoWomen {
private String notes;
// getters/setters omitted
}
@XmlRootElement(name = "persons")
class PersonDto {
private List<InfoMen> infoMen;
private List<InfoWomen> infoWomen;
@XmlElement(name = "notes")
public List<InfoMen> getInfoMen() {
return infoMen;
}
@XmlElement(name = "notes")
public List<InfoWomen> getInfoWomen() {
return infoWomen;
}
// setters omitted
}
我的XML輸出正常,但是我的PersonDto對象包含兩個對象。
<persons>
<notes>
<first>Petter</first>
<last>Butter</last>
<fullName>Petter Butter<fullName>
</notes>
<notes>
<first>Wenddy</first>
<last>Window</last>
<fullName>Wenddy Window<fullName>
</notes>
</persons>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.