/**
* 拷贝属性,但不适用null替换原属性
* @param source 源
* @return String[]
*/
public static String[] getNullPropertyNames (Object source) {
final BeanWrapper src = new BeanWrapperImpl(source);
java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();
Set<String> emptyNames = new HashSet<>();
for(java.beans.PropertyDescriptor pd : pds) {
Object srcValue = src.getPropertyValue(pd.getName());
if (srcValue == null) {
emptyNames.add(pd.getName());
}
}
String[] result = new String[emptyNames.size()];
return emptyNames.toArray(result);
}
public static void copyPropertiesIgnoreNull(Object src, Object target){
BeanUtil.copyProperties(src, target, getNullPropertyNames(src));
}
重写BeanUtils,不拷贝空值到对象
最新推荐文章于 2022-11-17 15:59:30 发布