java - MapStruct 无法映射需要外部变量的嵌套对象

标签 java lombok mapstruct

我正在尝试使用 MapStruct 来映射具有嵌套对象且需要外部变量的对象。

源 -> 目标映射是有损的,并且需要外部字符串

目标 -> 源映射工作并生成输出

我正在使用 Lombok,并且我的对象是不可变的。


//Entities
public class Repro {

    @Value
    @Builder
    public static class Nested {
        @NonNull
        private String id;
        @Nullable
        private String version;
        @NonNull
        private String externalId;
    }

    @Value
    @Builder
    public static class SourceEntity {
        @NonNull
        private String id;
        @NonNull
        private String anotherId;
    }

    @Value
    @Builder
    public static class TargetEntity {
        @NonNull
        private Nested nested;
        @NonNull
        private String anotherId;
    }
}
//Mapper

@Mapper
public interface ReproMapper {

    @Mapping(target = "nested.version", ignore = true)
    @Mapping(source = "source.id", target = "nested.id")
    @Mapping(source = "source.anotherId", target = "anotherId")
    @Mapping(source = "externalId", target = "nested.externalId")
    Repro.TargetEntity fromSource(Repro.SourceEntity source, String externalId);

    @Mapping(source = "nested.id", target = "id")
    @Mapping(source = "anotherId", target = "anotherId")
    Repro.SourceEntity fromTarget(Repro.TargetEntity target);
}

我收到错误消息(省略包名称):

Can't map property "Repro.SourceEntity source" to "Repro.Nested nested". Consider to declare/implement a mapping method: "Repro.Nested map(Repro.SourceEntity value)

这告诉我实现一个不可行的映射方法(因为它将构造一个部分嵌套对象),该方法在build()调用期间会失败。

有没有办法使用 MapStruct 解决这个问题,或者我只是实现自己的映射器?

最佳答案

你可以这样尝试(手写方法 icm @MappingContext 来传递 externalId:

@Mapper
public interface ReproMapper {

    @Mapping(target = "nested",source = "source")
    @Mapping(target = "anotherId",source = "source.anotherId")
    Repro.TargetEntity fromSource(Repro.SourceEntity source, @Context String externalId);

    //A default impl that delegates to MapStruct generated method
    default Repro.TargetEntity.Nested resolveNested(Repro.SourceEntity source, @Context String externalId) {
        return delegate(source, externalId);
    }

    //Actual Mapping method, generated by MapStruct
    //Note here externalId is not @Context annotated
    @Mapping(target = "version", ignore = true)
    @Mapping(target = "id", source = "source.id")
    @Mapping(target = "externalId", source = "externalId")
    Repro.TargetEntity.Nested delegate(Repro.SourceEntity source, String externalId)

    @Mapping(source = "nested.id", target = "id")
    @Mapping(source = "anotherId", target = "anotherId")
    Repro.SourceEntity fromTarget(Repro.TargetEntity target);
}

关于java - MapStruct 无法映射需要外部变量的嵌套对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56845204/

相关文章:

java - lombok 1.16.4 与 maven (jdk7) 不工作

java - mapstruct 未在双向 OneToMany 上正确设置关系

spring-boot - SpringBoot + Lombok + MapStruct 不能一起工作

java - 使用 Android Studio 3 RC2 时出现错误 "package lombok doesn' t 存在

java - Mapstruct - 字符串操作,但仅限于一个属性

java - jsp jSTL上不显示来自 Controller 的信息

java - 将 HashMap 存储在 SD 卡上

java - 为 hashcode、equals 和 toString 方法生成单元测试

java - 设计问题 - 为业务计算生成 SQL 查询

java - Java中的“特殊属性/属性”而不是getter/setter,以避免样板代码