[英]Map a collection using Mapstruct and specify method for a single instance?
i have a mapstruct class with the following functions我有一个 mapstruct class 具有以下功能
abstract CustomApplication convertCustomApplication(ApplicationDTO source);
abstract RegularApplication convertRegularApplication(ApplicationDTO source);
@Named("mainConverter")
public App mainConverter(ApplicationDTO source) {
return source.isCustom() ? convertCustomApplication(source) : convertRegularApplication(source);
}
//@Mapping( qualifiedByName = "mainConverter")
abstract public List<App> convertApplications(List<ApplicationDTO> applicationList);
Both CustomApplication and RegularApplication extend App, so I have mainConverter that calls the converter according to the isCustom field. CustomApplication 和 RegularApplication 都扩展了 App,所以我有 mainConverter 根据 isCustom 字段调用转换器。
The problem is that when i try to build this i get an ambiguous mapping error, because mainConverter, convertRegularApplication, and convertCustomApplication all map from ApplicationDTO to App (or subclass of App).问题是,当我尝试构建它时,我得到一个模棱两可的映射错误,因为 mainConverter、convertRegularApplication 和 convertCustomApplication 所有 map 从 ApplicationDTO 到 App(或 App 的子类)。 So convertApplications does not know what to use.
所以 convertApplications 不知道用什么。 Is there any way i can explicitly tell it to call mainConverter?
有什么办法可以明确告诉它调用 mainConverter? I wanted something like @Mapping( qualifiedByName = "mainConverter") that I put above convertApplications, (but it doesnt work of course) Does anyone know how I can do this?
我想要类似 @Mapping(qualifiedByName = "mainConverter") 的东西,我把它放在 convertApplications 上面,(但它当然不起作用)有谁知道我该怎么做? Thanks!
谢谢!
In order to achieve what you are looking for you will need to use IterableMapping#qualifiedByName
.为了实现您正在寻找的内容,您需要使用
IterableMapping#qualifiedByName
。
This property is meant to enhance the selection for the list.此属性旨在增强列表的选择。
eg例如
abstract CustomApplication convertCustomApplication(ApplicationDTO source);
abstract RegularApplication convertRegularApplication(ApplicationDTO source);
@Named("mainConverter")
public App mainConverter(ApplicationDTO source) {
return source.isCustom() ? convertCustomApplication(source) : convertRegularApplication(source);
}
@IterableMapping(qualifiedByName = "mainConverter")
abstract public List<App> convertApplications(List<ApplicationDTO> applicationList);
You are making your mainConverter
qualified method.您正在制作您的
mainConverter
合格方法。 However, it looks like that method already redirects to the more specific method.但是,看起来该方法已经重定向到更具体的方法。 Therefore I would suggest to qualify the other two methods and remove the qualification from the
mainConverter
.因此,我建议对其他两种方法进行限定,并从
mainConverter
中删除限定。 Then you won't need to do anything for the iterable mapping.然后你不需要为可迭代映射做任何事情。
eg例如
@Named("customApplication")
abstract CustomApplication convertCustomApplication(ApplicationDTO source);
@Named("regularApplication")
abstract RegularApplication convertRegularApplication(ApplicationDTO source);
public App mainConverter(ApplicationDTO source) {
return source.isCustom() ? convertCustomApplication(source) : convertRegularApplication(source);
}
abstract public List<App> convertApplications(List<ApplicationDTO> applicationList);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.