ModelMapper( http://modelmapper.org/ ) 是否支持排除属性?如果值为空。
我刚找到 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...());
}
});
就我而言,我有很多属性(property)和冗长。 如果它们全部为空,如何排除映射属性。 有没有更舒服的解决方案?
谢谢。
最佳答案
您可以使用以下配置将 ModelMapper 配置为忽略所有为 null 的属性:
modelMapper.getConfiguration().setPropertyCondition(Conditions.isNotNull());
它很有用,例如,对于目标对象的部分更新,您只想从源对象复制非空的那些属性。
关于java - 如何从 Modelmapper 中排除整个属性(如果它们为空),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45451025/