我用java Mapstruct合作,映射到实体DTO的
我想使用其他映射一个映射,与具有相同签名都实现相同的方法,正因为如此我得到“不明确的映射方法找到映射属性”
我已经尝试实现一个接口上的共享的方法,然后扩展上都映射器,但问题依旧接口
我猜我需要使用某种类型的预选赛。我搜索谷歌和官方文件中,但我无法弄清楚如何应用此TECHNIC
// 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);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句