Jackson 和泛型類型參考:處理巢狀泛型物件
Jackson JSON 函式庫允許對複雜物件結構進行強大的處理。然而,在自訂物件中使用泛型類型時會出現一個常見的挑戰。
問題陳述:
考慮以下程式碼:
<code class="java">public class MyWrapper<t> { private MyRequest<t> request; // ... getters and setters } public class MyRequest<t> { private List<t> myobjects; // ... getters and setters }</t></t></t></t></code>
通用方法嘗試將JSON 物件反序列化為MyWrapper 實例:
實例:<code class="java">public MyRequest<t> tester() { TypeReference<mywrapper>> typeRef = new TypeReference<mywrapper>>(); MyWrapper<t> requestWrapper = (MyWrapper<t>) JsonConverter.fromJson(jsonRequest, typeRef); return requestWrapper.getRequest(); }</t></t></mywrapper></mywrapper></t></code>在巢狀MyRequest 物件上呼叫getMyObject() 時會出現此問題。 Jackson 傳回一個 LinkedHashMap,而不是所需類型 T 的實例。發生這種情況是因為 Java 類型擦除在編譯期間刪除了類型資訊。
解決方案:
指定要傳回確切的類型 T,您需要向 Jackson 提供有關該類型的附加資訊。這可以透過使用 Jackson 映射器中的 JavaType 類別來實現。<code class="java">JavaType type = mapper.getTypeFactory(). constructCollectionType(List.class, Foo.class);</code>將 tester() 方法中的行替換為:
<code class="java">TypeReference<mywrapper>> typeRef = new TypeReference<mywrapper>>(); JavaType requestType = mapper.getTypeFactory(). constructCollectionType(MyRequest.class, type.getType()); MyWrapper<t> requestWrapper = (MyWrapper<t>) JsonConverter.fromJson(jsonRequest, requestType);</t></t></mywrapper></mywrapper></code>這現在指示 Jackson 期望給定類型 Foo 的列表。反序列化 JSON 物件時,它會將其轉換為正確的類型。 透過向 Jackson 提供此附加類型信息,您可以準確處理嵌套的泛型對象,確保檢索時對象的類型正確。
以上是如何使用 Jackson 反序列化巢狀通用物件:使用 TypeReference 和 JavaType 的指南的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Yes,Javacanruneverywhereduetoits"WriteOnce,RunAnywhere"philosophy.1)Javacodeiscompiledintoplatform-independentbytecode.2)TheJavaVirtualMachine(JVM)interpretsorcompilesthisbytecodeintomachine-specificinstructionsatruntime,allowingthesameJava

jdkincludestoolsfordEveloping and compilingjavacode,whilejvmrunsthecompiledbytecode.1)jdkcontainsjre,編譯器,andutilities.2)

Java的關鍵特性包括:1)面向對象設計,2)平台獨立性,3)垃圾回收機制,4)豐富的庫和框架,5)並發支持,6)異常處理,7)持續演進。 Java的這些特性使其成為開發高效、可維護軟件的強大工具。

JavaachievesPlatFormIndependencEthroughByTeCodeAndthejvm.1)sodiscompiledIntobyTecode,notmachinecode.2)thejvminterpretsbytbybytecodeonanyplatform,確保“ writeononce,runany where。”

Java在企業級應用中被廣泛使用是因為其平台獨立性。 1)平台獨立性通過Java虛擬機(JVM)實現,使代碼可在任何支持Java的平台上運行。 2)它簡化了跨平台部署和開發流程,提供了更大的靈活性和擴展性。 3)然而,需注意性能差異和第三方庫兼容性,並採用最佳實踐如使用純Java代碼和跨平台測試。

JavaplaysigantroleiniotduetoitsplatFormentence.1)itallowscodeTobewrittenOnCeandrunonVariousDevices.2)Java'secosystemprovidesuseusefidesusefidesulylibrariesforiot.3)

ThesolutiontohandlefilepathsacrossWindowsandLinuxinJavaistousePaths.get()fromthejava.nio.filepackage.1)UsePaths.get()withSystem.getProperty("user.dir")andtherelativepathtoconstructthefilepath.2)ConverttheresultingPathobjecttoaFileobjectifne

Java'splatFormIndenceistificantBecapeitAllowSitallowsDevelostWriTecoDeonCeandRunitonAnyPlatFormwithAjvm.this“ writeonce,runanywhere”(era)櫥櫃櫥櫃:1)交叉plat formcomplibility cross-platformcombiblesible,enablingDeploymentMentMentMentMentAcrAptAprospOspOspOssCrossDifferentoSswithOssuse; 2)


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

WebStorm Mac版
好用的JavaScript開發工具

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

Atom編輯器mac版下載
最受歡迎的的開源編輯器