ianaz
ianaz

Reputation: 2580

Spring Mongo convert to document from json string

I have a Mongo collection annotated with @Document and I want the possibility to also get that Java object from a String (JSON) as we're getting these classes pushed into a queue as String.

Is there a method in Spring-Data-Mongo which converts from JSON to the actual Document object?

Upvotes: 3

Views: 6322

Answers (2)

ianaz
ianaz

Reputation: 2580

@Autowired
MongoTemplate mongoTemplate;

and then

mongoTemplate.getConverter().read(MatchMongo.class, (DBObject) JSON.parse(json));

Thanks to freakman, your answer helped a lot

Upvotes: 7

rocky
rocky

Reputation: 5004

You can try com.mongodb.util.JSON.parse() method. It returns object so you probably have to do the casting + it may be it need "class" field inside json string.

Upvotes: 3

Related Questions