gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:42:57 25 4
gpt4 key购买 nike

我正在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com