Nested Target in MapStruct (Using Lombok) not working fluently

て烟熏妆下的殇ゞ 提交于 2019-12-24 19:16:19

问题


I am trying to use Mapstruct to do some nested mapping and with Lombok. As according to their latest specification mapstruct 1.2 works out of the box with Lombok.

Here is my code:

Source Classes

@Builder
@Getter
@Setter
public class PojoA {
   private String stringA;
   private int integer;
   private PojoB pojoB;
}

@Builder
@Getter
@Setter
public class PojoB {
    private long aLong;
    private String string;
    private PojoC pojoC;
}

@Builder
@Getter
@Setter
public class PojoC {
    private int value;
}

Target Classes

@Setter
@Getter
@ToString
public class Target {
    private String targetString;
    private int integer;
    private TargetPrime targetPrime;
}

@ToString
@Getter
@Setter
public class TargetPrime {
    private long aLong;
    private String string;
    private TargetPrimePrime targetPrimePrime;
}

@ToString
@Getter
@Setter
public class TargetPrimePrime {
    private int value;
}

Mapper

@Mapper
public interface PojoMapper {

    PojoMapper INSTANCE = Mappers.getMapper(PojoMapper.class);

    @Mapping(source = "stringA", target = "targetString")
    @Mapping(source = "pojoB", target = "targetPrime")
    Target pojoAToTarget(PojoA pojoA);

    TargetPrimePrime map(PojoC pojoC);

    @Mapping(source = "pojoC", target = "targetPrimePrime")
    TargetPrime mapToPrime(PojoB pojoB);
}

As it is written that MapStruct will now work with Lombok out of the box, this means that the mapping code should work. But instead, it gives me a compilation error "that it could not find the setter setTargetPrime".

But surprisingly if I add getter/setter only for targetPrime and keep rest as it is then it works fine with Lombok.

For eg.

Updated Target

@Setter
@Getter
@ToString
public class Target {
    private String targetString;
    private int integer;
    private TargetPrime targetPrime;

    public TargetPrime getTargetPrime() {
        return targetPrime;
    }

    public void setTargetPrime(TargetPrime targetPrime) {
        this.targetPrime = targetPrime;
    }
}

And run this afterward

PojoC pojoC = PojoC.builder().value(12345).build();
        PojoB pojoB = PojoB.builder().aLong(15).string("AutoScanned").pojoC(pojoC).build();
        PojoA pojoA = PojoA.builder().integer(10).stringA("Testing").pojoB(pojoB).build();
        Target target = PojoMapper.INSTANCE.pojoAToTarget(pojoA);
        System.out.println(target);

and I will get successful output with all the mappings.

OUTPUT

Target(targetString=Testing, integer=10, targetPrime=TargetPrime(aLong=15, string=AutoScanned, targetPrimePrime=TargetPrimePrime(value=12345)))

So, As I can't remove Lombok from all the projects that I am working on because my team likes Lombok a lot so, Does Lombok really works out of the box for mapping (mutable objects) and if yes, what am I doing wrong in the above code?

来源:https://stackoverflow.com/questions/48753526/nested-target-in-mapstruct-using-lombok-not-working-fluently

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!