java Bean拷贝忽略空属性

  • java bean的拷贝一般采用两种办法
    1.Spring里面的方法, org.springframework.beans.BeanUtils.copyProperties
    2.Apache里面的方法,org.apache.commons.beanutils.BeanUtils.copyProperties
    但是有需要注意的地方,Spring中BeanUtils.copyProperties(A,B)是将A赋值给B
    Apache中BeanUtils.copyProperties(A,B)是将B赋值给A.
    对于拷贝时空属性的处理,下面是我的方法,采用了Spring的方法
public static Object copyBeanIgnoreNullProperties(Object sourceObj, Object destObj) {
        if (sourceObj == null || destObj == null) {
            log.error("copyBean参数缺失!原参数为空?{},目标参数为空?{}", sourceObj == null, destObj == null);
            return destObj;
        }
        try {

            BeanUtils.copyProperties(sourceObj,destObj,getNoValuePropertyNames(sourceObj));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return destObj;
    }

    private static String[] getNoValuePropertyNames (Object source) {
        Assert.notNull(source, "传递的参数对象不能为空");
        final BeanWrapper beanWrapper = new BeanWrapperImpl(source);
        PropertyDescriptor[] pds = beanWrapper.getPropertyDescriptors();
        Set<String> noValuePropertySet = new HashSet<>();
        Arrays.stream(pds).forEach(pd -> {
            Object propertyValue = beanWrapper.getPropertyValue(pd.getName());
            if (StringUtils.isEmpty(propertyValue)) {
                noValuePropertySet.add(pd.getName());
            } else {
                if (Iterable.class.isAssignableFrom(propertyValue.getClass())) {
                    Iterable iterable = (Iterable) propertyValue;
                    Iterator iterator = iterable.iterator();
                    if (!iterator.hasNext()) noValuePropertySet.add(pd.getName());
                }
                if (Map.class.isAssignableFrom(propertyValue.getClass())) {
                    Map map = (Map) propertyValue;
                    if (map.isEmpty()) noValuePropertySet.add(pd.getName());
                }
            }
        });
        String[] result = new String[noValuePropertySet.size()];
        return noValuePropertySet.toArray(result);
    }
BeanUtils.copyProperties()方法在属性复制过程,默认情况下是不会忽略任何属性的。即当两个对象的属性名相同但类型不同时,该属性值不会被拷贝。 然而,如果我们想要在复制属性忽略某些属性,可以使用一些特定的方法来实现。在Spring框架,我们可以使用BeanUtils.copyProperties(Object source, Object target, String... ignoreProperties)方法来实现属性忽略。我们可以将要忽略属性名称作为参数传递给该方法。这样,在属性复制过程,指定的属性将被忽略,不会被拷贝到目标Bean。 举个例子,假设我们有两个对象source和target,它们都有属性名为"name"和"type",但是source对象的"type"属性是字符串类型,而target对象的"type"属性是整型。我们希望在属性复制过程忽略"type"属性,我们可以这样使用BeanUtils.copyProperties()方法来实现: BeanUtils.copyProperties(source, target, "type"); 这样,属性复制过程,"type"属性将被忽略,只有"name"属性的值会被拷贝到目标Bean。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [java BeanUtils.copyProperties()对象拷贝忽略某个属性](https://blog.csdn.net/qq_44209563/article/details/123806208)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [springframework.beans.BeanUtils.copyProperties和apache.commons.beanutils.BeanUtils.copyProperties的...](https://blog.csdn.net/weixin_47297386/article/details/131956666)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值