我正在嘗試使用Jackson與Kotlin反序列化Json字符串到類型爲OperationResult<String>
的對象。如何從類型名稱中獲取類名?
我需要構建一個類型的對象,像這樣:
val mapper : ObjectMapper = ObjectMapper();
val type : JavaType = mapper.getTypeFactory()
.constructParametricType(*/ class of OperationResult */,,
/* class of String */);
val result : OperationResult<String> = mapper.readValue(
responseString, type);
我試過以下,但他們沒有工作。
val type : JavaType = mapper.getTypeFactory()
.constructParametricType(
javaClass<OperationResult>,
javaClass<String>); // Unresolved javaClass<T>
val type : JavaType = mapper.getTypeFactory()
.constructParametricType(
OperationResult::class,
String::class);
如何從類型名稱中獲得java類?
您應該使用Jackson-Kotiln模塊,然後不需要這些。 https://github.com/FasterXML/jackson-module-kotlin –
@JaysonMinard謝謝。我已經下載並使用Maven構建了一個jar。我將繼續使用它。 –
它在Maven Central中有工件,無需構建它... https://mvnrepository.com/artifact/com.fasterxml.jackson.module/jackson-module-kotlin/2.8.2 –