本文介绍了Jersey序列化/反序列化问题:抽象类型只能使用其他类型信息进行实例化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用平针织进行序列化和反序列化。我使用泽西在WebLogic上创建了REST通道。我有包含抽象类的结果对象。 Jersey使用此类实现名称添加结果元数据:

I'm using jersey for both serialization and deserialization. I've made REST channel on WebLogic using jersey. I have result object with contains abstract class. Jersey adds to the result metadata with this class'es implementation name:

{"order":{"@type":"installationOrder",

然而,当用于反序列化此数据时,相同的球衣会尖叫以下内容:

However, the same jersey, when using to deserialize this data, is screaming the following:

Caused by: org.codehaus.jackson.map.JsonMappingException: Can not construct instance of ocl.mobile.service.data.order.DetailedOrder, problem: abstract types can only be instantiated with additional type information
 at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@97eded; line: 1, column: 2] (through reference chain: ocl.mobile.service.OrderDetailsResult["order"])
    at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)
    at org.codehaus.jackson.map.deser.StdDeserializationContext.instantiationException(StdDeserializationContext.java:212)
    at org.codehaus.jackson.map.deser.AbstractDeserializer.deserialize(AbstractDeserializer.java:97)
    at org.codehaus.jackson.map.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:252)
    at org.codehaus.jackson.map.deser.SettableBeanProperty$MethodProperty.deserializeAndSet(SettableBeanProperty.java:356)
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:494)
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:350)
    at org.codehaus.jackson.map.ObjectMapper._readValue(ObjectMapper.java:2376)
    at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1166)
    at org.codehaus.jackson.jaxrs.JacksonJsonProvider.readFrom(JacksonJsonProvider.java:410)
    at com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy.readFrom(JacksonProviderProxy.java:139)
    at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:553)
    ... 5 more

但他本人已经在他序列化的JSON中提供了这些额外信息。

but he himself have provided this additional information in the JSON he has serialized.

那么,如何让球衣阅读并理解他创造的这种@type注释?

So, how to make jersey to read and understand this "@type" annotations he have created?

这就是我使用球衣从频道读取数据的方式:

This is how I'm using jersey to read data from channel:

private static Client client;

private static void initClient() {
    if (client == null) {
        ClientConfig clientConfig = new DefaultClientConfig();
        clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING,
                Boolean.TRUE);
        client = Client.create(clientConfig);
    }
}

private static <T> T jsonForResult(String addr, Class<T> expectedClass) {
    initClient();
    WebResource r = client.resource(addr);
    try {
        T result = r.get(expectedClass);
    return result;
        } catch (UniformInterfaceException e) {
        log.error(e.getMessage(), e);
        return null;
    }
}

在我的情况下,expectedClass是结果类,其中包含状态和抽象类order,其中包含installationOrder等实现。

The expectedClass is in my case the class of result, which contains status and the abstract class "order", which has implementations such as "installationOrder".

推荐答案

泽西岛(或更具体地说) ,它与POJO映射一起使用的Jackson JSON lib)除非启用了类型信息包含,否则不会添加 @type ,通常是通过添加 @JsonTypeInfo 在抽象类型上。所以必须启用这个功能。也许你可以分享定义 DetailOrder class?

Jersey (or more specifically, Jackson JSON lib it uses with POJO mapping) does not add @type unless type information inclusion is enabled, usually by adding @JsonTypeInfo on an abstract type. So something must have enabled this. Maybe you can share definition DetailOrder class?

至于问题本身:这通常是由于使用了不兼容的类型 - - 用于反序列化的类型(将JSON值读入POJO)必须是 @JsonTypeInfo 注释可见。例如,您不能只询问 java.lang.Object 类型的值,因为它没有这样的注释。在不知道实际的类定义的情况下,不可能指出具体原因,但这是最可能的解释。

As to problem itself: this is usually caused by incompatible types used -- type used for deserialization (reading JSON value into POJO) must be such that @JsonTypeInfo annotation is visible. You can not, for example, just ask for value of type java.lang.Object, since it does not have such annotation. Without knowing actual class definitions it is not possible to point to specific cause, but this is the most likely explanation.

这篇关于Jersey序列化/反序列化问题:抽象类型只能使用其他类型信息进行实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 18:09