Java 如何在MapStruct中禁用源映射中的字段?

Java 如何在MapStruct中禁用源映射中的字段?,java,mapstruct,Java,Mapstruct,例如,我有一个带有字段的mapping类,该字段未显示在mapping类中 我要映射的类: @Entity @Table(name = "t_connection") @Getter @Setter @EqualsAndHashCode public class ConnectionEntity { @NotNull @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id")

例如,我有一个带有字段的mapping类,该字段未显示在mapping类中

我要映射的类:

@Entity
@Table(name = "t_connection")
@Getter @Setter
@EqualsAndHashCode
public class ConnectionEntity {
    @NotNull
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    protected UUID id;
...
}
@ApiModel
@Getter
@Setter
@NoArgsConstructor
public class ConnectionDto {
    @ApiModelProperty
    private LocalDateTime createAt;
...
// Other fields without id field
}
我要在其中映射的类:

@Entity
@Table(name = "t_connection")
@Getter @Setter
@EqualsAndHashCode
public class ConnectionEntity {
    @NotNull
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    protected UUID id;
...
}
@ApiModel
@Getter
@Setter
@NoArgsConstructor
public class ConnectionDto {
    @ApiModelProperty
    private LocalDateTime createAt;
...
// Other fields without id field
}
我的映射器如下所示:

@Mapper(componentModel = "spring",
        unmappedTargetPolicy = ReportingPolicy.ERROR,
        unmappedSourcePolicy = ReportingPolicy.ERROR)
public interface CallMapper {

    @IterableMapping(qualifiedByName = "map")
    List<ConnectionDto> map(List<ConnectionEntity> connectionEntities);

    ConnectionDto map(ConnectionEntity connectionEntity);
}

我想知道,当某个特定字段未映射时,禁用unmappedSourcePolicy不是一个选项。有什么建议吗?

如果我理解你的话。。是否要控制不希望映射的源属性

在这种情况下,请尝试:

@beanmappingNoreunmappedSourceProperties

因此:

@MappercomponentModel=spring, UnappedTargetPolicy=ReportingPolicy.ERROR, unmappedSourcePolicy=ReportingPolicy.ERROR 公共接口调用映射器{ @IterableMappingqualifiedByName=map 列表映射列表连接; @BeanMapping ignoreUnmappedSourceProperties={id} 连接到地图连接性连接性; }
您不需要指定列表映射,除非您需要从外部指定。。MapStruct将为您的..生成一个。。如果确实需要外部列表,则可能不需要限定符。。通用+列表已经足够了

似乎不可能了,有一种相当愚蠢的方法就是手工编写地图。默认ConnectionTo mapConnectionEntity connectionEntity{ConnectionTo dto=新建ConnectionTo;dto.set…connectionEntity.get…;return dto;}这是可能的。感谢@sjaak。看看这个答案: