如何使用 Jackson 的 objectMapper 反序列化接口字段? [英] How to deserialize interface fields using Jackson's objectMapper?

查看:30
本文介绍了如何使用 Jackson 的 objectMapper 反序列化接口字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ObjectMapperreadValue(InputStream in, Class valueType) 函数需要 Class.但是如果我在内部传递的类有一些接口作为数据成员,我该如何使用它.

ObjectMapper's readValue(InputStream in, Class<T> valueType) function requires the Class. But how do I use it if the class I am passing internally, is having some Interface as data member.

虽然我可以理解这个异常背后的原因,因为Jackson没有得到传递类的内部接口的具体类,但我的问题是如何解决它?那我该如何反序列化呢?我试图反序列化的类是:

although I can understand the reason behind this exception, as Jackson is not getting the concrete class of the internal Interface of the passed class, but my question is how to resolve it? how do I deserialize it then? The class I am trying to deserialize is:

class BaseMetricImpl<N> implements Metric<N> {
    protected MetricValueDescriptor descriptor;
}

这里 MetricValueDescriptor 是一个接口,所以这给了我以下错误:-

Here MetricValueDescriptor is an interface, so this gives me following error : -

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: java.io.ByteArrayInputStream@2ede2c9f; 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)

推荐答案

Jackson 显然不能构造 MetricValueDescriptor 对象,因为它是一个接口.您将需要在 json 和 ObjectMapper 中包含其他信息,以告诉 jackson 如何从中构造对象.这是一种方法,假设 MVDImpl 是一个实现 MetricValueDescriptor 的具体类:

Jackson obviously cannot construct the MetricValueDescriptor object since it is an interface. You will need to have additional information in your json and in your ObjectMapper to tell jackson how to construct an object out of it. Here is one way to do it, assuming MVDImpl is a concrete class which implements MetricValueDescriptor:

您可以通过 json 本身中的字段告诉 Jackson 所需的类型信息,例如 "type".为此,您需要使用 JsonTypeInfoJsonSubTypes 界面中的注释.例如,

You can tell Jackson the required type information through a field in the json itself, say "type". To do this, you need to use JsonTypeInfo and JsonSubTypes annotations in your interface. For example,

@JsonTypeInfo(
    use = JsonTypeInfo.Id.NAME,
    include = JsonTypeInfo.As.PROPERTY,
    property = "type")
@JsonSubTypes({
    @Type(value = MVDImpl.class, name = "mvdimpl") })
interface MetricValueDescriptor
{
   ...
}

您还需要在 json 中添加 "type":"mvdimpl" 字段.

You will need to add a "type":"mvdimpl" field in your json as well.

我打算将您指向官方文档以了解更多信息,但后来我发现了一个很好的博客,涵盖了这个主题 - 使用 Jackson 反序列化 JSON.它非常全面地涵盖了这个主题,并附有示例.因此,如果您需要更多定制,您一定要阅读它.

I was going to point you to the official doc for more info, but then I found an excellent blog covering this topic - Deserialize JSON with Jackson. It covers this topic pretty comprehensively and with examples. So you should definitely read it if you need more customisation.

这篇关于如何使用 Jackson 的 objectMapper 反序列化接口字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆