BeanUtils copyProperties API to ignore null and sp

2019-04-06 11:52发布

Spring's BeanUtils.copyProperties() provides option to ignore specific properties while copying beans:

public static void copyProperties(Object source,
                 Object target,
                 String[] ignoreProperties) throws BeansException

Does the Apache Commons BeanUtils provide a similar feature?

Also is it possible to ignore null values while using Spring's BeanUtils.copyProperties(), I see this feature with Commons BeanUtils:

Date defaultValue = null;
DateConverter converter = new DateConverter(defaultValue);
ConvertUtils.register(converter, Date.class);

Can I achieve the same with Spring's BeanUtils?

2条回答
三岁会撩人
2楼-- · 2019-04-06 12:29

If you want to ignore null-value you have to do it with the following line of code before copying properties:

BeanUtilsBean.getInstance().getConvertUtils().register(false, false, 0);
查看更多
我只想做你的唯一
3楼-- · 2019-04-06 12:35

In case you are using the org.springframework.beans.BeanUtils you can ignore specific properies using the method copyProperties(Object source, Object target, String... ignoreProperties). An example,

BeanUtils.copyProperties(sourceObj, targetObj, "aProperty", "another");
查看更多
登录 后发表回答