我正在使用球衣进行序列化和反序列化.我使用jersey在WebLogic上创建了REST通道.我有结果对象包含抽象类. Jersey使用此类的实现名称添加结果元数据:
{"order":{"@type":"installationOrder",
但是,同样的球衣,当用于反序列化这些数据时,会尖叫以下内容:
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中提供了这些额外的信息.
那么,如何让球衣阅读并理解他创造的这种“@type”注释?
这就是我使用球衣从频道读取数据的方式:
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; } }
expectClass在我的例子中是结果类,它包含状态和抽象类“order”,它具有诸如“installationOrder”之类的实现.