java - Jersey 序列化/反序列化问题 : abstract types can only be instantiated with additional type information

标签 java json jersey jackson deserialization

我正在使用 Jersey 进行序列化和反序列化。我已经使用 Jersey 在 WebLogic 上创建了 REST channel 。我有包含抽象类的结果对象。 Jersey 使用此类的实现名称添加到结果元数据中:

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

但是,同样的 Jersey ,在用于反序列化这些数据时,尖叫着以下内容:

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 中提供了此附加信息。

那么,如何让 jersey 阅读和理解他创建的这个“@type”注释呢?

这就是我使用 Jersey 从 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”的实现。

最佳答案

尝试 this它有效

@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = ExchangeFormat.class, name = "ExchangeFormat"),
@JsonSubTypes.Type(value = TypeStatus.class, name = "TypeStatus"),
})
public abstract class MapperJsonXml <T>

xml也是一样

@XmlSeeAlso({ExchangeFormat.class,TypeStatus.class})

关于java - Jersey 序列化/反序列化问题 : abstract types can only be instantiated with additional type information,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9066288/

相关文章:

java - 在 libgdx 中渲染 box2d

Java 11.javac/警告 : [options] module name in --add-reads option not found: moduleB

java - Realm java,如何通过字符串访问 RealmObject 属性(反射)

java - Swing - 停止图像闪烁

json - 如何将 <?, ?> 类型的 json 文件加载到 DropdownButton

c# - MVC RedirectToAction 在 JSON 后返回后不起作用

java - 如何使用 Restful Web 服务使用两种 Web 方法进行两种不同的操作?

c# - 将 NServiceBus 事件从 v6 JSON 发布到 v4 XML 环境

xml - Web 服务响应的 text/xml 与 application/xml 之间有什么区别

java - 以编程方式将 Jersey REST 服务附加到 Jetty