Java – Jersey serialization / deserialization problem: abstract types can only be instantiated with other type information
I'm serializing and deserializing my jersey I used Jersey to create a rest channel on Weblogic I have result objects that contain abstract classes Jersey uses the implementation name of this class to add result metadata:
{"order":{"@type":"installationOrder",
However, the same Jersey, when used to deserialize these data, will scream 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
But he himself has provided this additional information in his serialized JSON
So, how to make the Jersey read and understand the "@ type" annotation he created?
This is how I use my jersey to read data from the 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; } }
Expectclass is the result class in my example. It contains the state and abstract class "order", which has an implementation such as "installationorder"
Solution
Jersey (or more specifically, Jackson JSON lib, which is used with POJO mapping) will not add @ type unless type information inclusion is enabled, usually by adding @ jsontypeinfo. On the abstract type Therefore, this function must be enabled Maybe you can share and define the detailorder class?
As for the problem itself: This is usually caused by incompatible types used - the type used for deserialization (reading JSON values into POJOs) must make the @ jsontypeinfo annotation visible For example, you can't just ask Java Value of type lang.Object because it has no such comment Without knowing the actual class definition, it is impossible to point out the specific reason, but this is the most likely explanation