A small problem in the deserialization of objects in MongoDB

Source: Internet
Author: User
Tags mongodb client

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.