如果从Modelmapper中为null,则如何排除整个属性 [英] how to exclude whole property if they are null from Modelmapper

查看:1719
本文介绍了如果从Modelmapper中为null,则如何排除整个属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

喜欢这个。

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?

谢谢。

推荐答案

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

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

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

例如,对于只想复制那些目标对象的目标对象的部分更新很有用源对象中的属性不为null。

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.

这篇关于如果从Modelmapper中为null,则如何排除整个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆