【发布时间】:2017-10-03 15:03:41
【问题描述】:
我正在使用 Apache Commons BeanUtils 将一些属性从源 Bean 复制到目标 Bean。在此,我不想在来自源 bean 的目标 bean 中设置空值。
例如:
Person sourcePerson = new Person();
sourcePerson.setHomePhone("123");
sourcePerson.setOfficePhone(null);
Person destPerson = new Person();
destPerson.setOfficePhone("456");
BeanUtils.copyProperties(destPerson, sourcePerson);
System.out.println(destPerson.getOffcePhone());
//这里destPerson officePhone设置为null
如何避免这种情况?我什至尝试提出以下声明:
BeanUtilsBean.getInstance().getConvertUtils().register(false, false, 0);
这似乎没有帮助。
我们可以在 Apache Commons BeanUtils 中排除空值吗?
【问题讨论】:
-
您可以使用不会尝试转换属性的 PropertyUtils...否则您需要注册 ConvertUtils.register 以获得默认值...
-
我不想要任何默认值,我只是不想用 null 覆盖现有值。我还是不明白 PropertyUtils 是如何排除空值的。
-
@StanislavL ignoreProperties 选项不能解决这个问题,ConvertUtils 也没有,如问题中所述。
标签: java spring mapping apache-commons-beanutils