当我尝试在 mapstruct
中的两个类
之间创建一个映射器
时,
编译代码时,我收到警告
:
src/main/java/mapstruct/DogMapper.java:15: warning: Unmapped target property: "otherField".
Cat convert(Dog dog);
^
1 warning
这是我试图在之间映射的两个对象:
狗
@Getter
@Setter
public class Dog {
private String say;
}
猫
@Getter
@Setter
public class Cat {
private String say;
private String otherField;
}
这是我的映射器
@Mapper
public interface DogMapper {
DogMapper mapper = Mappers.getMapper( DogMapper.class );
@Mapping(source = "say", target = "say")
Cat convert(Dog dog);
}
我读了mapstruct docs
,而且我知道我可以通过多种方式排除这个特定字段:
@Mapping(ignore = true, target = "otherField")
或者通过这种方式:
@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)
但我最终的目的是排除名为 otherField
的特定字段,
来 self 所有的映射器,但不排除我未使用的其他字段。
有什么办法可以实现这一点吗?
最佳答案
你已经回答了你自己的问题,我不确定我是否理解正确。您只想输入 @Mapping(ignore = true, target = "otherField")
一次吗?
如果此字段位于某个公共(public)基类中,您可以使用 Shared Configurations 。否则,您使用 @Mapping(ignore = true)
的方式就是正确的选择。
附注。不必添加@Mapping(source = "say", target = "say")
MapStruct自动映射同名属性
关于java - 排除mapstruct中的特定字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48509231/