ObjectMapper
的readValue(InputStream in, Class<T> valueType)
函數需要該類。但是如果我在內部傳遞的類有一些接口作爲數據成員,我該如何使用它。如何使用Jackson的objectMapper對接口字段進行反序列化?
雖然我能理解這個異常背後的原因,因爲傑克遜沒有獲得傳遞類的內部接口的具體類,但我的問題是如何解決它? 那我該如何反序列化呢?我想反序列化的類是:
class BaseMetricImpl<N> implements Metric<N> {
protected MetricValueDescriptor descriptor;
}
這裏MetricValueDescriptor
是一個接口,所以這給了我以下錯誤: -
com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of MetricValueDescriptor, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information
at [Source: [email protected]; line: 1, column: 2] (through reference chain: SingleValueMetricImpl["descriptor"])
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
at com.fasterxml.jackson.databind.DeserializationContext.instantiationException(DeserializationContext.java:624)
at com.fasterxml.jackson.databind.deser.AbstractDeserializer.deserialize(AbstractDeserializer.java:115)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:375)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:98)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:308)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2793)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1989)
你可以更新你的示例代碼並完成錯誤堆棧跟蹤。 – vels4j
@ vels4j我編輯了這個問題,把小代碼示例和異常 – Naman