I would like to know how to copy the properties from an Object Source to an Object Dest ignoring null values using Spring Framework.
I actually use Apache beanutil
A better solution based on Pawel Kaczorowski's answer:
public static String[] getNullPropertyNames(Object source) {
final BeanWrapper wrappedSource = new BeanWrapperImpl(source);
return Stream.of(wrappedSource.getPropertyDescriptors())
.map(FeatureDescriptor::getName)
.filter(propertyName -> {
try {
return wrappedSource.getPropertyValue(propertyName) == null
} catch (Exception e) {
return false
}
})
.toArray(String[]::new);
}
For example, if we have a DTO:
class FooDTO {
private String a;
public String getA() { ... };
public String getB();
}
Other answers will throw exceptions on this special case.