0

将 java 对象存储为 json 时,@class将添加到数据中,并将用于反序列化到相应的对象中。

但是当Kotlin涉及到一个数据类时,它没有添加@class,当我尝试反序列化时,我得到了错误

Caused by: com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Missing type id when trying to resolve subtype of [simple type, class java.lang.Object]: missing type id property '@class'
 at [Source: (byte[])"{"Integer":1}"; line: 1, column: 13]
    at com.fasterxml.jackson.databind.exc.InvalidTypeIdException.from(InvalidTypeIdException.java:43)
    at com.fasterxml.jackson.databind.DeserializationContext.missingTypeIdException(DeserializationContext.java:1771)
    at com.fasterxml.jackson.databind.DeserializationContext.handleMissingTypeId(DeserializationContext.java:1300) 

我无法添加注释@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS),因为这会暴露我的数据类,并且我知道错误是因为 Kotlin 数据类默认情况下是最终的。

有没有办法克服这个错误?

4

0 回答 0