(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);
}
}
}
}
}
}
}