3

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?

2 Answers 2

7
@Autowired
MongoTemplate mongoTemplate;

and then

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

Thanks to freakman, your answer helped a lot

Sign up to request clarification or add additional context in comments.

Comments

3

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.

1 Comment

I get a BasicDbObject with the JSON.parse() method and the casting doesn't work... I posted the answer :) Thank you +1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.