使用助手 class 时出现 mapstruct 不明确的异常
mapstruct ambiguous exception when using helper class
使用 mapstruct v1.0.0.Final,我在尝试从 SourceType
映射到 TargetType
时遇到不明确的映射异常:
class TargetType {
List<TargetTypeChild> children;
boolean allResults;
}
class SourceType {
List<SourceTypeChild> children;
boolean allResults;
}
我使用的映射是:
@Mapper(uses = B.class)
interface A {
@Mapping(target = "children", source = "children", qualifiedBy = ToTargetType.class)
TargetType toTargetType (SourceType source);
@Mapping(target = "children", source = "children", qualifiedBy = ToTargetTypeNoDetails.class)
TargetType toTargetTypeNoDetails (SourceType source);
}
interface B {
@Qualifier
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface ToTargetType {}
@Qualifier
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface ToTargetTypeNoDetails {}
@ToTargetType
@IterableMapping(qualifiedBy = ToTargetType.class)
List<TargetTypeChild> withDetails(List<SourceTypeChild> value);
@ToTargetTypeNoDetails
@IterableMapping(qualifiedBy = ToTargetTypeNoDetails.class)
List<TargetTypeChild> noDetails(List<SourceTypeChild> value);
@ToTargetType
@Mappings({
@Mapping(target = "details", source = "details"),
...rest of mapping
})
TargetTypeChild toTargetTypeChild(SourceTypeChild source);
@ToTargetTypeNoDetails
@Mappings({
@Mapping(target = "details", ignore = true),
...rest of mapping
})
TargetTypeChild toTargetTypeChildNoDetails(SourceTypeChild source);
}
这不会编译,在两个接口 A 的方法中都会出现以下异常:
Ambiguous mapping methods found for mapping property "List children" to List: List noDetails(List arg0), List withDetails(List arg0).
对此有一个解决方法:将接口 A 的两个方法都放在接口 B 中。这样可以编译并运行。但出于商业原因,我需要将它们分开。
谁能解释为什么第一种方法不起作用而变通方法却起作用?
作为奖励问题,如果我只编写 1 种映射方法(无限定符),我什至不需要声明 @IterableMapping
方法,mapstruct 知道如何找到 "children"方法。
为什么?
谢谢大家!
Anyone could explain why the first approach doesn't work and the workaround does?
您的限定符注释必须至少有保留策略 CLASS
,只有这样它们才会被发现。如果所有内容都在同一个源文件中定义,则不需要这样做,在这种情况下 SOURCE
就足够了。
As a bonus question, if I only code 1 method for mapping (no qualifiers)
MapStruct 将根据需要生成(私有)可迭代映射方法。实际上它也应该适用于您的原始情况,这似乎是我们需要修复的一个小故障。我已经为此提交了问题#707。
感谢您报告此事!
使用 mapstruct v1.0.0.Final,我在尝试从 SourceType
映射到 TargetType
时遇到不明确的映射异常:
class TargetType {
List<TargetTypeChild> children;
boolean allResults;
}
class SourceType {
List<SourceTypeChild> children;
boolean allResults;
}
我使用的映射是:
@Mapper(uses = B.class)
interface A {
@Mapping(target = "children", source = "children", qualifiedBy = ToTargetType.class)
TargetType toTargetType (SourceType source);
@Mapping(target = "children", source = "children", qualifiedBy = ToTargetTypeNoDetails.class)
TargetType toTargetTypeNoDetails (SourceType source);
}
interface B {
@Qualifier
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface ToTargetType {}
@Qualifier
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface ToTargetTypeNoDetails {}
@ToTargetType
@IterableMapping(qualifiedBy = ToTargetType.class)
List<TargetTypeChild> withDetails(List<SourceTypeChild> value);
@ToTargetTypeNoDetails
@IterableMapping(qualifiedBy = ToTargetTypeNoDetails.class)
List<TargetTypeChild> noDetails(List<SourceTypeChild> value);
@ToTargetType
@Mappings({
@Mapping(target = "details", source = "details"),
...rest of mapping
})
TargetTypeChild toTargetTypeChild(SourceTypeChild source);
@ToTargetTypeNoDetails
@Mappings({
@Mapping(target = "details", ignore = true),
...rest of mapping
})
TargetTypeChild toTargetTypeChildNoDetails(SourceTypeChild source);
}
这不会编译,在两个接口 A 的方法中都会出现以下异常:
Ambiguous mapping methods found for mapping property "List children" to List: List noDetails(List arg0), List withDetails(List arg0).
对此有一个解决方法:将接口 A 的两个方法都放在接口 B 中。这样可以编译并运行。但出于商业原因,我需要将它们分开。
谁能解释为什么第一种方法不起作用而变通方法却起作用?
作为奖励问题,如果我只编写 1 种映射方法(无限定符),我什至不需要声明 @IterableMapping
方法,mapstruct 知道如何找到 "children"方法。
为什么?
谢谢大家!
Anyone could explain why the first approach doesn't work and the workaround does?
您的限定符注释必须至少有保留策略 CLASS
,只有这样它们才会被发现。如果所有内容都在同一个源文件中定义,则不需要这样做,在这种情况下 SOURCE
就足够了。
As a bonus question, if I only code 1 method for mapping (no qualifiers)
MapStruct 将根据需要生成(私有)可迭代映射方法。实际上它也应该适用于您的原始情况,这似乎是我们需要修复的一个小故障。我已经为此提交了问题#707。
感谢您报告此事!