DozerBeanMapper:忽略源

时间:2017-02-23 12:26:27

标签: java mapping dozer

在我的Java项目中,我使用org.dozer.BeanMapper将复杂的源对象合并到具有相同类型的目标对象中:

sourceObject.valueA = null
sourceObject.valueB = B1
sourceObject.valueC = C1

destinationObject.valueA = A2
destinationObject.valueB = null
destinationObject.valueC = C2

beanMerger.map(sourceObject, destinationObject);

这导致我的destinationObject中的值:

destinationObject.valueA: null
destinationObject.valueB: B1
destinationObject.valueC: C1 

我可以配置BeanMapper,它不应该覆盖空值的字段吗?我的预期结果应该是:

destinationObject.valueA: A2
destinationObject.valueB: B1
destinationObject.valueC: C1 

甚至更好(不要触摸目标对象的设定值)

destinationObject.valueA: A2
destinationObject.valueB: B1
destinationObject.valueC: C2

1 个答案:

答案 0 :(得分:1)

我找到了解决方案:

beanMerger.addMapping(
  new BeanMappingBuilder() {
    protected void configure() {
       mapping(MyObjectDTO.class, MyObjectDTO.class, TypeMappingOptions.mapNull(false));
    }
  });