BeanUtils copyProperties API to ignore null and specific propertie

Arun picture Arun · Jul 2, 2013 · Viewed 35.5k times · Source

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?

Answer

George Siggouroglou picture George Siggouroglou · May 24, 2016

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");