排除 mapstruct 中的特定字段
exclude specific fields in mapstruct
当我尝试在 mapstruct
中的两个 classes
之间创建一个 mapper
时,
我在编译代码时得到 warning
:
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
@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
,
的特定字段
来自我所有的映射器,但不排除我没有使用的其他字段。
有什么方法可以实现吗?
你已经回答了你自己的问题,我不确定我是否理解正确。您只想输入一次 @Mapping(ignore = true, target = "otherField")
吗?
如果此字段在某些公共基础 class 中,您可以使用 Shared Configurations。否则,您使用 @Mapping(ignore = true)
的方法就是正确的方法。
边注。您不必添加 @Mapping(source = "say", target = "say")
MapStruct 会自动映射具有相同名称的属性
当我尝试在 mapstruct
中的两个 classes
之间创建一个 mapper
时,
我在编译代码时得到 warning
:
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
@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
,
的特定字段
来自我所有的映射器,但不排除我没有使用的其他字段。
有什么方法可以实现吗?
你已经回答了你自己的问题,我不确定我是否理解正确。您只想输入一次 @Mapping(ignore = true, target = "otherField")
吗?
如果此字段在某些公共基础 class 中,您可以使用 Shared Configurations。否则,您使用 @Mapping(ignore = true)
的方法就是正确的方法。
边注。您不必添加 @Mapping(source = "say", target = "say")
MapStruct 会自动映射具有相同名称的属性