作者:絕色天龍
來源:www.jianshu.com/p/357b55852efc

背景
最近專案中在和第三方進行聯調一個介面,我們這邊發送http請求給對方,然后接收對方的回應,代碼都是老代碼,
根據注釋,對方的SDK中寫好的Request類有一個無法序列化的bug,所以這邊重新寫了一個Request類,基本屬性都是相同的,但是重點是有一個屬性是靜態內部類,還有兩個是list屬性,
類似于下面這樣:
private List orders;
private AddRequest.Ticket ticket;
private List payments;
AddRequest就是我們自己重寫的請求類,他們SDK中的請求類是MixAddRequest,我們組裝好請求引數后利用Spring的BeanUtils的copyProperties方法將AddRequest中的屬性拷貝到MixAddRequest,然后發送請求,
到此為止,照理說一切完美!
結果請求失敗,納尼?對方說缺少一個必要的欄位,引數校驗不通過!
一查欄位名稱,是Ticket這個類里面的某個欄位,趕緊看代碼,心里充滿對老代碼的自信,想著一定是哪里搞錯了,或者是他們那邊偷偷動了代碼,把欄位從可選改為了必選,嘿嘿,
果然在代碼里找到了設定的地方,這下應該是他們的問題確信無疑了,再開一把除錯,準備宣判他們的死刑,結果發現發給他們的請求就是沒有這個欄位,,,
中間只有一個Spring的copy屬性的方法,當時覺得很詭異,由于中間只有這么一行代碼,玄機肯定在這里面,初步懷疑是兩個靜態內部類不同導致,所以自己寫Demo,準備搞一把這個BeanUtils的copyProperties方法
寫了兩個類和一個Main,@Data和@ToString是lombok插件的注解,這里用來自動生成getter和setter方法以及toString方法,
@ToString
@Data
public class CopyTest1 {
public String outerName;
public CopyTest1.InnerClass innerClass;
public List clazz;
@ToString
@Data
public static class InnerClass {
public String InnerName;
}
}
@ToString
@Data
public class CopyTest2 {
public String outerName;
public CopyTest2.InnerClass innerClass;
public List clazz;
@ToString
@Data
public static class InnerClass {
public String InnerName;
}
}
CopyTest1 test1 = new CopyTest1();
test1.outerName = "hahaha";
CopyTest1.InnerClass innerClass = new CopyTest1.InnerClass();
innerClass.InnerName = "hohoho";
test1.innerClass = innerClass;
System.out.println(test1.toString());
CopyTest2 test2 = new CopyTest2();
BeanUtils.copyProperties(test1, test2);
System.out.println(test2.toString());
這里遇到了第一個坑,一開始圖省事,屬性寫為public,想著省掉了getter和setter方法,沒加@Data注解,結果運行完test2所有屬性都為null,一個都沒copy過去,加上@Data繼續跑,果然,基本屬性(String)復制過去了,但是內部類在test2中還是null,推薦閱讀:推薦一款代碼神器,代碼量至少省一半,
那就驗證了真的是內部類的問題,有點不敢相信自己的眼睛,畢竟線上跑了這么久的代碼,,,
知道了問題,總要想著怎么解決吧,所以需要單獨設定一下內部類,單獨copy,
如果內部類的bean屬性較多或者遞回的bean屬性很多,那可以自己封裝一個方法,用于遞回拷貝,我這里只有一層,所以直接額外copy一次,
CopyTest1 test1 = new CopyTest1();
test1.outerName = "hahaha";
CopyTest1.InnerClass innerClass = new CopyTest1.InnerClass();
innerClass.InnerName = "hohoho";
test1.innerClass = innerClass;
System.out.println(test1.toString());
CopyTest2 test2 = new CopyTest2();
test2.innerClass = new CopyTest2.InnerClass();
BeanUtils.copyProperties(test1, test2);
BeanUtils.copyProperties(test1.innerClass, test2.innerClass);
System.out.println(test2.toString());
記得內部類的屬性也是要有setter方法的,不然也會導致copy失敗,大家還記得我開頭說到還有兩個List屬性的吧,為什么要提到這個呢?你猜
其實list里面的兩個類也都是重寫的內部類,他們也是不同的,當時他們卻順利copy過去了
為什么呢?因為java的泛型只在編譯期起作用,在運行期,list屬性就是一個存放Object的集合
在copy后,MixAddRequest的orders屬性其實是一個Order類的集合,但卻不是自己內部類的集合,是AddRequest的內部類Order的集合,但因為對方是決議json的,所以沒有發生錯誤,,,
總結
-
Spring的BeanUtils的CopyProperties方法需要對應的屬性有getter和setter方法;
-
如果存在屬性完全相同的內部類,但是不是同一個內部類,即分別屬于各自的內部類,則spring會認為屬性不同,不會copy;
-
泛型只在編譯期起作用,不能依靠泛型來做運行期的限制;
-
最后,spring和apache的copy屬性的方法源和目的引數的位置正好相反,所以導包和呼叫的時候都要注意一下,
最后的最后
附上spring的原始碼,getWriteMethod是jdk的方法,會去取set開頭的方法,所以沒有setter方法是不行滴,
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) {
PropertyDescriptor targetPd = var7[var9];
Method writeMethod = targetPd.getWriteMethod();
if (writeMethod != null && (ignoreList == null || !ignoreList.contains(targetPd.getName()))) {
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 = https://www.cnblogs.com/javastack/p/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);
}
}
}
}
}
}
推薦去我的博客閱讀更多:
1.Java JVM、集合、多執行緒、新特性系列教程
2.Spring MVC、Spring Boot、Spring Cloud 系列教程
3.Maven、Git、Eclipse、Intellij IDEA 系列工具教程
4.Java、后端、架構、阿里巴巴等大廠最新面試題
覺得不錯,別忘了點贊+轉發哦!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/162464.html
標籤:Java
下一篇:Java--JDBC控制事務
