Mapstruct:如何映射层次结构,其中子对象是列表并且具有列表属性
我有一个场景,需要从 DTO 对象映射到域对象。
我已经简化了下面的场景
class TopLevel {
private String attr1;
private String attr2;
private List<SecondLevel> secondLevel = new ArrayList<>();
class SecondLevel {
private String attr3;
private JsonNode additionalConfig;
private List<String> stringList = new ArrayList<>();
private List<ThirdLevel> thirdLevel = new ArrayList<>();
class ThirdLevel {
private String attr4;
private String attr5;
private List<String> anotherList = new ArrayList<>();
}
}
}
这需要映射到下面的域结构
class TopLevelDomain {
private ID guid1; //Generated Guid
private String attr1;
private String attr2;
private List<SecondLevelDomain> secondLevelDomain = new ArrayList<>();
}
class SecondLevelDomain {
private ID guid1; //From TopLevel Domain
private ID guid2; //Generated Guid
private String attr3;
private String stringListAsString; // List of Strings needs to be converted to string (JSON format)
private String additionalConfig; //JSON node needs to be converted to String (JSON format)
private List<ThirdLevelDomain> thirdLevelDomain = new ArrayList<>();
}
class ThirdLevelDomain {
private ID guid1; //From TopLevel Domain
private ID guid2; //From SecondLevel Guid
private ID guid3; //New Guid
private String attr4;
private String attr5;
private String anotherListAsString; // List of Strings needs to be converted to string (JSON format)
}
现在我需要进行qualifiedByName/Named映射来获取第一级的GUID。另外,我还需要其他 2 个子级别的类似 GUID。
@Mapping(source = "topLevel", target = "guid1", qualifiedByName = "mapGuid1")
@Mapping(source = "topLevel", target = "secondLevelDomain", qualifiedByName = "secondLevelDomain")
TopLevelDomain toTopLevel(TopLevel topLevel)
如果我执行类似上面代码的操作,我看不出如何访问第二级属性 attr3、additionalConfig 和 @Mapping 注释中的列表 stringList
另外,如何访问第三级属性 attr4、attr5 和 anotherList。我需要直接映射字符串矩阵,分配 guid 并为字符串和 JSONNode 列表进行一些自定义映射
I have a scenario where I need to map from a DTO object to a Domain objects.
I have simplified the scenario below
class TopLevel {
private String attr1;
private String attr2;
private List<SecondLevel> secondLevel = new ArrayList<>();
class SecondLevel {
private String attr3;
private JsonNode additionalConfig;
private List<String> stringList = new ArrayList<>();
private List<ThirdLevel> thirdLevel = new ArrayList<>();
class ThirdLevel {
private String attr4;
private String attr5;
private List<String> anotherList = new ArrayList<>();
}
}
}
This needs to be mapped to the domain structure below
class TopLevelDomain {
private ID guid1; //Generated Guid
private String attr1;
private String attr2;
private List<SecondLevelDomain> secondLevelDomain = new ArrayList<>();
}
class SecondLevelDomain {
private ID guid1; //From TopLevel Domain
private ID guid2; //Generated Guid
private String attr3;
private String stringListAsString; // List of Strings needs to be converted to string (JSON format)
private String additionalConfig; //JSON node needs to be converted to String (JSON format)
private List<ThirdLevelDomain> thirdLevelDomain = new ArrayList<>();
}
class ThirdLevelDomain {
private ID guid1; //From TopLevel Domain
private ID guid2; //From SecondLevel Guid
private ID guid3; //New Guid
private String attr4;
private String attr5;
private String anotherListAsString; // List of Strings needs to be converted to string (JSON format)
}
Now I need to do a qualifiedByName/Named mapping to get the GUID for the first level. Also I need similar GUID for the other 2 child levels as well.
@Mapping(source = "topLevel", target = "guid1", qualifiedByName = "mapGuid1")
@Mapping(source = "topLevel", target = "secondLevelDomain", qualifiedByName = "secondLevelDomain")
TopLevelDomain toTopLevel(TopLevel topLevel)
If I do something like the above code, I see no way how I can access the secondlevel attributes attr3, additionalConfig and the List stringList in the @Mapping annotation
Also how do I access the thirdlevel attributes attr4, attr5 and anotherList. I need to to direct mapping for the string mattributes, assign the guids and do some custom mapping for the List of String and JSONNodes
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想要在这里做两件事。
There are 2 things that you want to do here.
@AfterMapping
to pass on the generated GUID's.