在我的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

最佳答案

我找到了解决方案:

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

关于java - DozerBeanMapper:忽略源中的空值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42415739/

10-13 05:00