gpt4 book ai didi

java - DozerBeanMapper : Ignore Null values from Source

转载 作者:行者123 更新时间:2023-12-01 08:50:53 26 4
gpt4 key购买 nike

在我的 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 : Ignore Null values from Source,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42415739/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com