MapStruct 不为标有特定注释的字段生成映射

MapStruct to not generate mappings for fields marked with specific annotation

我正在处理无法修改的框架代码,它最终在映射期间抛出 NullPointerException,因为 MapStruct 认为它应该使用在 superclass 中定义的 getter .

有没有办法告诉 MapStruct 忽略所有标有 @JsonIgnore(jackson 库注释)的 getter?


更多上下文

为了提供一些代码,这里是 MapStruct 生成的实现的一部分:

        if ( target.getChangedProperties() != null ) {
            target.getChangedProperties().clear();
            List<Property> list = src.getChangedProperties();
            if ( list != null ) {
                target.getChangedProperties().addAll( list );
            }
        }

NPE 是从 target.getChangedProperties() 内部抛出的,因为有一些未初始化的变量正在被访问。然而,实际上,我什至不希望这个 getter 成为 MapStruct 实现的一部分。 (实际上,getter 不是特定变量的 getter,而是更多的“实用程序 getter”,所以我想知道为什么 MapStruct 试图使用它。)

我映射的 class 看起来像:

@Entity
@Table(name = "myentity")
@JsonIgnoreProperties(ignoreUnknown = true)
@Data
@NoArgsConstructor
@AllArgsConstructor
public class MyEntity extends TheFrameworkSuperClass {

  @Id
  private String id;

  private String foo;
}
@MappedSuperclass
@JsonSerialize(include = JsonSerialize.Inclusion.NON_DEFAULT)
@JsonIgnoreProperties(ignoreUnknown = true)
public abstract class TheFrameworkSuperClass {

    @Version
    @JsonProperty(value = "Version")
    private Long version;

    @Transient
    @JsonIgnore
    protected UnitOfWorkChangeSet changes;

    @Override
    @JsonIgnore
    public List<Property> getChangedProperties() {
        // stuff happening before
        this.changes.getObjectChangeSetForClone(this); // throws NPE
        // stuff happening after
    }
}

我的 MapStruct 接口

我没有自定义映射器的配置。我的界面是:

@Mapper(componentModel = "spring")
public interface MyMapper {

    MyEntity boToBo(MyEntity destination);

    void updateBo(MyEntity src, @MappingTarget MyEntity target);
}

我考虑使用 @BeanMapping(ignoreByDefault = true) 然后单独列出每个字段以确保没有使用额外的 getter,但由于我使用的次数太多,这远不是一个令人满意的解决方案我必须这样做。

好吧,事实证明即使 changedProperties 字段不存在,因为 MapStruct 将 getChangedProperties() 作为 getter,你仍然可以告诉 MapStruct 忽略那个不存在的领域...

@Mapper(componentModel = "spring")
public interface MyMapper {

    @Mapping(target = "changedProperties", ignore = true)
    MyEntity boToBo(MyEntity destination);

    @Mapping(target = "changedProperties", ignore = true)
    void updateBo(MyEntity src, @MappingTarget MyEntity target);
}