I am implementing a RESTful server inside my company. The workflow is very straight-forward: request and response. However, nothing is easy, the RESTful service is based on an existed project, which handles database operations, and the entities inside that project is pretty complex(a lot of interfaces and abstract classes).
Take this as a demo:
In class Zoo, there is a set of IAnimal. The when I tried to deserialize {"animals":[{"name":"1"},{"name":"2"}]} into Zoo instance, it will will have such error as below:
1 2 3 4 5 6 7
com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.oshackers.quickstart.jackson.model.IAnimal, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information at [Source: {"animals":[{"name":"1"},{"name":"2"}]}; line: 1, column: 13] (through reference chain: com.oshackers.quickstart.jackson.model.Zoo["animals"]->java.util.HashSet[0]) at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148) at com.fasterxml.jackson.databind.DeserializationContext.instantiationException(DeserializationContext.java:857) at com.fasterxml.jackson.databind.deser.AbstractDeserializer.deserialize(AbstractDeserializer.java:139) at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:245)
The error message is very clear.
(Note: For Jackson version 1.x, you may have other error, as find object as LinkedHashMap, cannot cast to SomeClass.)