简体   繁体   English

使用java Mapstruct的不明确的映射方法

[英]Ambiguous mapping methods using java Mapstruct

I'm working with java Mapstruct to mapping Entities to DTOs 我正在使用java Mapstruct将实体映射到DTO

I want to use one mapper from other mapper and both implement the same method with the same signature and because of that I'm getting "Ambiguous mapping methods found for mapping property" 我想使用其他映射器中的一个映射器,并且都使用相同的签名实现相同的方法,因此我得到“为映射属性找到的模糊映射方法”

I have already tried to implement the shared method on an interface and then extend the interface on both mappers but the problem remains 我已经尝试在接口上实现共享方法,然后在两个映射器上扩展接口,但问题仍然存在

I'm guessing I will need to use some kind of qualifier. 我猜我需要使用某种限定符。 I searched on google and in the official documentation but I can't figure it out how to apply this 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);
}
}

Using a qualifier is one way to solve this. 使用限定符是解决此问题的一种方法。 However, in your case the problem is the fromInstant method which is actually an util method. 但是,在您的情况下,问题是fromInstant方法,它实际上是一个util方法。

Why don't you extract that method to some static util class and tell both mapper to use that class as well? 为什么不将该方法提取到某个静态util类并告诉mapper也使用该类?

public class MapperUtils {

    public static OffsetDateTime fromInstant(Instant instant) {
        return instant == null ? null : instant.atOffset(ZoneOffset.UTC);
    }
}

Then your mappers can look like: 然后您的地图制作者可能看起来像:

@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);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM