Today in MongoDB access to the object data, encountered a small problem: the object of a certain field type is abstract class or interface, when the deposit is not a problem, but at the time of reading, because there is no specific class of information, unable to complete the object reconstruction, will be error:
Can not construct instance of com.bp.samples.json.generics.xxx, problem:abstract types either need to being mapped to Concre Te types, with custom Deserializer, or is instantiated with additional type Informationat [source:xxxxx]
Search, one way is to add annotations to the parent class, indicating the names of all the specific classes that inherit the parent class, URL:
http://blog.csdn.net/java_huashan/article/details/46428971
However, this method is limited by a limitation, if my specific subclass is extensible, then each time you want to modify the source code above the annotations. If the parent class is in the framework, it may be inappropriate in the jar package.
By the guidance of the people, find another good way, is in the http://wiki.fasterxml.com/JacksonPolymorphicDeserialization mentioned in the enabledefaulttyping part, When the MongoDB client initializes, the Objectmapper object calls this method to reconstruct the member variables of the abstract class declaration.
Like this: Mapper = new Objectmapper ();
Mapper.enabledefaulttyping ();
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
A small problem in the deserialization of objects in MongoDB