how to exclude whole property if they are null from Modelmapper

自古美人都是妖i 提交于 2019-12-06 12:31:24

问题


Does ModelMapper(http://modelmapper.org/) support what exclude property? If the value is null.

I just found PropertyMap out. but It is a constraint to me. because I have to describe a specific property that I want.

Like this.

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...());
    }
});

In my case, I have a lot of property and verbose. How to exclude mapping property if they are null from all them. Is there more comfortable solution?

thanks.


回答1:


You can configure ModelMapper to ignore all properties that are null with the following configuration:

modelMapper.getConfiguration().setPropertyCondition(Conditions.isNotNull());

It is useful, for example, for partial updates of a target object where you only want to copy those properties from the source object that are not null.



来源:https://stackoverflow.com/questions/45451025/how-to-exclude-whole-property-if-they-are-null-from-modelmapper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!