Sunday, 18 August 2013

How to return raw JSON directly from a mongodb query in Java?

How to return raw JSON directly from a mongodb query in Java?

I have the following code:
@RequestMapping(value = "/envinfo", method = RequestMethod.GET)
@ResponseBody
public Map getEnvInfo()
{
BasicQuery basicQuery = new
BasicQuery("{_id:'51a29f6413dc992c24e0283e'}", "{'envinfo':1, '_id':
false }");
Map envinfo= mongoTemplate.findOne(basicQuery, Map.class, "jvmInfo");
return envinfo;
}
As you can notice, the code:
Retrieves JSON from MongoDB
Converts it to a Map object
The Map object is then converted to JSON by Spring MongoData before it is
returned to the browser.
Is it possible to directly return the raw json from MongoDb without going
through the intermediate conversion steps?

No comments:

Post a Comment