0
点赞
收藏
分享

微信扫一扫

java BeanUtils为原属性为空时不赋值

楚木巽 2022-02-16 阅读 69
public static String[] getNullPropertyNames (Object source) {
   final BeanWrapper src = new BeanWrapperImpl(source);
   PropertyDescriptor[] pds = src.getPropertyDescriptors();
   Set<String> emptyNames = new HashSet<String>();
   for(PropertyDescriptor pd : pds) {
      Object srcValue = src.getPropertyValue(pd.getName());
      if (srcValue == null) emptyNames.add(pd.getName());
      if(srcValue!=null&&srcValue instanceof String){
         if(StringUtil.isBlank((String)srcValue)){
            emptyNames.add(pd.getName());
         }
      }
   }
   String[] result = new String[emptyNames.size()];
   return emptyNames.toArray(result);
}
BeanUtils.copyProperties(srcData, targetData,getNullPropertyNames(srcData));

举报

相关推荐

0 条评论