自定义Bean拷贝工具类解决字段名称不一致的拷贝

本文介绍了一种解决BeanUtils.copyProperties方法在拷贝多层复杂数据结构时遇到的问题的方法,即字段名不一致导致的拷贝失败。通过自定义注解和工具类实现了字段名映射,成功完成了数据拷贝。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(2条消息) 属性名称不一致,实现 bean copyProperties_专业摸鱼的博客-CSDN博客

最近在处理第三方接口返回的数据,然后要将这些数据保存到数据库中。可是接口返回的数据的结构十分的复杂,涉及多层的数据结构。

{
    "echoCode": 0,
    "success": true,
    "page": 0,
    "pageSize": 0,
    "pageCount": 1,
    "total": 1,
    "data": [
        {
            "associateShop": {
                "tenant": "x",
                "id": "x",
                "orgType": "-",
                "orgId": "-",
                "code": "x",
                "name": "x",
                "type": "x",
                "contact": {
                    "telephone": "x"
                },
                "address": {
                    "provinceName": "x",
                    "districtName": "x",
                    "streetName": "x"
                },
                "enabled": true
            },
            "code": "x",
            "name": "x",
            "state": "x",
            "businessState": "x",
        }
    ]
}

例如我要拿到 telephone,provinceName,districtName等字段就比较麻烦了。

不过在我的机智聪明下,首先把这个json数据转为gson对象,然后就可以通过对象拿到字段值了。

这时候又有另外一个问题了:本来想通过 BeanUtils.copyProperties 拷贝属性值到数据库entity对象,发现比较深层的字段拷贝失败,原因是字段名不一致

翻看代码发现,原理是:

会根据target的字段名称去source的属性字段查找,如果有就会返回一个sourcePd的对象(类型为PropertyDescriptor),然后这个对象会拿到 readMethod对象,这个对象可以拿到字段的值;同理,target也会拿到targetPd(类型为PropertyDescriptor),这个对象会拿到 writeMethod 对象,可以给字段设置值。

private static void copyProperties(Object source, Object target, @Nullable Class<?> editable, @Nullable String... ignoreProperties) throws BeansException {
    Assert.notNull(source, "Source must not be null");
    Assert.notNull(target, "Target must not be null");
    Class<?> actualEditable = target.getClass();
    if (editable != null) {
        if (!editable.isInstance(target)) {
            throw new IllegalArgumentException("Target class [" + target.getClass().getName() + "] not assignable to Editable class [" + editable.getName() + "]");
        }

        actualEditable = editable;
    }

    PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);
    List<String> ignoreList = ignoreProperties != null ? Arrays.asList(ignoreProperties) : null;
    PropertyDescriptor[] var7 = targetPds;
    int var8 = targetPds.length;

    for(int var9 = 0; var9 < var8; ++var9) {
        //获取到target的字段对象
        PropertyDescriptor targetPd = var7[var9];
        //然后拿到写的方法
        Method writeMethod = targetPd.getWriteMethod();
        if (writeMethod != null && (ignoreList == null || !ignoreList.contains(targetPd.getName()))) {
            //获取到source的字段对象,拷贝的关键,如果字段名不一致无法拷贝
            PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());
            if (sourcePd != null) {
                //拿到读的方法
                Method readMethod = sourcePd.getReadMethod();
                if (readMethod != null && ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) {
                    try {
                        if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
                            readMethod.setAccessible(true);
                        }

                        Object value = readMethod.invoke(source);
                        if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
                            writeMethod.setAccessible(true);
                        }

                        //写入值
                        writeMethod.invoke(target, value);
                    } catch (Throwable var15) {
                        throw new FatalBeanException("Could not copy property '" + targetPd.getName() + "' from source to target", var15);
                    }
                }
            }
        }
    }

}

这个方法最关键的是:PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName()),如果字段名不一致无法拷贝。

为了解决这个问题,主要通过自定义一个注解,自定义一个拷贝工具类,然后通过修改这段关键代码的字段名来完成拷贝。

主要参考:(2条消息) 属性名称不一致,实现 bean copyProperties_专业摸鱼的博客-CSDN博客

注解

@Inherited
@Target({ElementType.ANNOTATION_TYPE,ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface CopySourceName {

    String value();
}

工具类

public class CopyBeanUtil {

    public static void copyProperties(Object source, Object target) throws BeansException {
        copyProperties(source, target, (Class)null, (String[])null);
    }

    private static void copyProperties(Object source, Object target, @Nullable Class<?> editable, @Nullable String... ignoreProperties) throws BeansException {
        Assert.notNull(source, "Source must not be null");
        Assert.notNull(target, "Target must not be null");
        Class<?> actualEditable = target.getClass();
        if (editable != null) {
            if (!editable.isInstance(target)) {
                throw new IllegalArgumentException("Target class [" + target.getClass().getName() + "] not assignable to Editable class [" + editable.getName() + "]");
            }

            actualEditable = editable;
        }

        PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);
        List<String> ignoreList = ignoreProperties != null ? Arrays.asList(ignoreProperties) : null;
        PropertyDescriptor[] var7 = targetPds;
        int var8 = targetPds.length;

        //获取自定义注解的值
        HashMap<String, String> copySourceNameMap = new HashMap<>();
        for (Field field : actualEditable.getDeclaredFields()) {
            CopySourceName annotation = field.getAnnotation(CopySourceName.class);
            if (annotation != null) {
                copySourceNameMap.put(field.getName(), annotation.value());
            }
        }

        for(int var9 = 0; var9 < var8; ++var9) {
            PropertyDescriptor targetPd = var7[var9];
            Method writeMethod = targetPd.getWriteMethod();
            String name = targetPd.getName();
            String sourceFieldName = copySourceNameMap.get(name);
            //将需要拷贝的字段名称保持一致
            if (sourceFieldName != null) {
                name = sourceFieldName;
            }
            if (writeMethod != null && (ignoreList == null || !ignoreList.contains(name))) {
                //将需要拷贝的字段名称保持一致
                PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), name);
                if (sourcePd != null) {
                    Method readMethod = sourcePd.getReadMethod();
                    if (readMethod != null && ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) {
                        try {
                            if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
                                readMethod.setAccessible(true);
                            }

                            Object value = readMethod.invoke(source);
                            if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
                                writeMethod.setAccessible(true);
                            }

                            writeMethod.invoke(target, value);
                        } catch (Throwable var15) {
                            throw new FatalBeanException("Could not copy property '" + targetPd.getName() + "' from source to target", var15);
                        }
                    }
                }
            }
        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值