我正在尝试使用Mapstruct来执行一些嵌套映射和使用Lombok。根据他们最新的规范,mapstruct 1.2与Lombok一起开箱即用。
这是我的代码:
来源类
@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;
}
目标类
@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
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);
}
正如编写的那样,MapStruct现在可以与Lombok一起使用,这意味着映射代码应该可以工作。 但相反,它给了我一个编译错误"它无法找到setter setTargetPrime"。
但令人惊讶的是,如果我只为targetPrime添加getter / setter并保持休息,那么它与Lombok一起工作正常。
例如。
更新目标
@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;
}
}
然后运行
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);
我将获得所有映射的成功输出。
输出
目标(targetString = Testing,integer = 10,targetPrime = TargetPrime(aLong = 15,string = AutoScanned,targetPrimePrime = TargetPrimePrime(value = 12345)))
所以,因为我不能从我正在处理的所有项目中删除Lombok因为我的团队非常喜欢Lombok,所以Lombok真的开箱即用于映射(可变对象),如果是的话,我在上面的代码中做错了什么?