如果从Modelmapper中为null,则如何排除整个属性

Bra*_*ady 1 java mapper modelmapper

ModelMapper(http://modelmapper.org/)是否支持排除属性的内容?如果该值为null.

我刚刚找到了PropertyMap.但这对我来说是一个约束.因为我必须描述我想要的特定属性.

像这样.

ModelMapper modelMapper = new ModelMapper();
modelMapper.addMappings(new PropertyMap<TestObject, TestObject>() {
    @Override
    protected void configure() {
        when(Conditions.isNull()).skip().setName(source.getName());
        when(Conditions.isNull()).skip().set...(source.get...());
        when(Conditions.isNull()).skip().set...(source.get...());
        when(Conditions.isNull()).skip().set...(source.get...());
        when(Conditions.isNull()).skip().set...(source.get...());
        when(Conditions.isNull()).skip().set...(source.get...());
    }
});
Run Code Online (Sandbox Code Playgroud)

在我的情况下,我有很多财产和冗长.如果映射属性从它们中为空,如何排除它们.有更舒适的解决方案吗?

谢谢.

map*_*apm 7

您可以使用以下配置将ModelMapper配置为忽略所有null属性:

modelMapper.getConfiguration().setPropertyCondition(Conditions.isNotNull());
Run Code Online (Sandbox Code Playgroud)

例如,对于目标对象的部分更新很有用,在目标对象中,您只想从源对象复制非空的那些属性.