小编Max*_*Max的帖子

Mapstruct 映射:如果所有源参数属性都为空,则返回空对象

如果@Mapping/source 中引用的所有属性都为null,我希望生成的mapstruct 映射方法返回null。例如,我有以下映射:

@Mappings({
      @Mapping(target = "id", source = "tagRecord.tagId"),
      @Mapping(target = "label", source = "tagRecord.tagLabel")
})
Tag mapToBean(TagRecord tagRecord);
Run Code Online (Sandbox Code Playgroud)

生成的方法是:

public Tag mapToBean(TagRecord tagRecord) {
    if ( tagRecord == null ) {
        return null;
    }

    Tag tag_ = new Tag();

    if ( tagRecord.getTagId() != null ) {
        tag_.setId( tagRecord.getTagId() );
    }
    if ( tagRecord.getTagLabel() != null ) {
        tag_.setLabel( tagRecord.getTagLabel() );
    }

    return tag_;
}
Run Code Online (Sandbox Code Playgroud)

测试用例:TagRecord 对象不为空,但具有 tagId==null 和 tagLibelle==null。

当前行为:返回的 Tag 对象不为 null,但具有 tagId==null 和 tagLibelle==null

如果(tagRecord.getTagId() == …

java object-object-mapping mapstruct

4
推荐指数
1
解决办法
6269
查看次数

标签 统计

java ×1

mapstruct ×1

object-object-mapping ×1