在开发中使用BeanUtils.copyProperties()方法的时候会出现Null的情况,为了规避此类情况可以使用以下方法。
public class nullPropertyNames { /** * 使用BeanUtils.copyProperties()复制时,去null值 * * @param source 对象 * @return @return {@link String[] } */ public static String[] nullPropertyNames(Object source) { final BeanWrapper src = new BeanWrapperImpl(source); PropertyDescriptor[] propertyDescriptor = src.getPropertyDescriptors(); Set<String> emptyNames = new HashSet<>(); for (PropertyDescriptor pd : propertyDescriptor) { Object srcValue = src.getPropertyValue(pd.getName()); if (srcValue == null) emptyNames.add(pd.getName()); } String[] result = new String[emptyNames.size()]; return emptyNames.toArray(result); } }