我正在尝试使用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 –