使用java Mapstruct的不明确映射方法
我正在使用JavaMapStruct将实体映射到DTO 我想使用另一个映射器中的一个映射器,并使用相同的签名实现相同的方法,因此我得到了“为映射属性找到的不明确映射方法” 我已经尝试在接口上实现共享方法,然后在两个映射器上扩展接口,但问题仍然存在 我猜我需要使用某种限定符。我在谷歌和官方文档中搜索过,但我不知道如何应用这项技术使用java Mapstruct的不明确映射方法,java,mapstruct,Java,Mapstruct,我正在使用JavaMapStruct将实体映射到DTO 我想使用另一个映射器中的一个映射器,并使用相同的签名实现相同的方法,因此我得到了“为映射属性找到的不明确映射方法” 我已经尝试在接口上实现共享方法,然后在两个映射器上扩展接口,但问题仍然存在 我猜我需要使用某种限定符。我在谷歌和官方文档中搜索过,但我不知道如何应用这项技术 // CHILD MAPPER *** @Mapper(componentModel = "spring", uses = { }) public interface C
// CHILD MAPPER ***
@Mapper(componentModel = "spring", uses = { })
public interface CustomerTagApiMapper {
CustomerTagAPI toCustomerTagApi(CustomerTag customerTag);
default OffsetDateTime fromInstant(Instant instant) {
return instant == null ? null : instant.atOffset(ZoneOffset.UTC);
}
}
// PARENT MAPPER ***
@Mapper(componentModel = "spring", uses = { CustomerTagApiMapper.class })
public interface CustomerApiMapper {
CustomerAPI toCustomerApi(Customer customer);
default OffsetDateTime frmInstant(Instant instant) {
return instant == null ? null : instant.atOffset(ZoneOffset.UTC);
}
}
使用限定符是解决此问题的一种方法。但是,在您的例子中,问题在于
fromInstant
方法,它实际上是一个util方法
为什么不将该方法提取到某个静态util类中,并告诉两个映射器也使用该类呢
public class MapperUtils {
public static OffsetDateTime fromInstant(Instant instant) {
return instant == null ? null : instant.atOffset(ZoneOffset.UTC);
}
}
然后,您的映射器可以如下所示:
@Mapper(componentModel = "spring", uses = { MapperUtils.class })
public interface CustomerTagApiMapper {
CustomerTagAPI toCustomerTagApi(CustomerTag customerTag);
}
@Mapper(componentModel = "spring", uses = { CustomerTagApiMapper.class, MapperUtils.class })
public interface CustomerApiMapper {
CustomerAPI toCustomerApi(Customer customer);
}
这太棒了。你的答案应该是显而易见的,但今天是第一次接触这个班级。。。。多谢各位