Search code examples
spring-mongospring-mongodb

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?


Solution

  • @Autowired
    MongoTemplate mongoTemplate;
    

    and then

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

    Thanks to freakman, your answer helped a lot