千萬要小心BeanUtils中的坑
- 2019 年 10 月 31 日
- 筆記
背景
最近項目中在和第三方進行聯調一個介面,我們這邊發送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 = 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); } } } } } }
來自:www.jianshu.com/p/357b55852efc