Mapstruct 检查空值

Mapstruct check null

我想在方法中检查空值

@Override
 public void updateFooFromNonNullAttributesOfDto(FooDto fooDto, Foo foo) {
        if ( fooDto== null ) {
            return;
        }
        if ( fooDto.getBar() != null ) {
            site.setBar( fooDto.getBar() );
        }
         if ( fooDto.getBaz() != null ) {
            site.setBar( fooDto.getBaz() );
        }
}

当我使用

@Mapper( NullValueCheckStrategy.ALWAYS)

它检查所有方法,但我只想检查一个... 如何解决这个问题?

您可以使用一些自定义的忽略策略。

讨论来源:[link][1]

ignoreStrategy 的可能值:

ALWAYS (as true)
NEVER (as false)
NULL_SOURCE (ignore if source value is null) - often required in many projects
    if (source.getValue() != null)
        target.setValue (source.getValue());
NULL_TARGET (ignore if target value is null)
    if (target.getValue() != null)
        target.setValue(source.getValue());

[1]: https://github.com/mapstruct/mapstruct/issues/369 ???

错误?我可以修改对方的答案????

这对于 MapStruct 尚不可行,您可以为所有或 none 做。已经有一个完全相同的功能请求。参见 1243