Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  1. Retrieves JSON from MongoDB
  2. Converts it to a Map object
  3. 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?

like image 753
pdeva Avatar asked Aug 18 '13 23:08

pdeva


People also ask

How does spring boot store JSON objects in MongoDB?

To store raw json object/array, all you have to do is to declare the type as "Object" in the Pojo and/or DTO level on your server side. The "Object" type will work with Spring Data and MapStruct too. Then on the client side, you can send your json data as a json data.

Can MongoDB store JSON?

Anything you can represent in JSON can be natively stored in MongoDB, and retrieved just as easily in JSON.


1 Answers

There's two way's you can do this right now:

1. Using the CollectionCallback on MongoTemplate

You can use a CollectionCallback to deal with the returned DBObject directly and simply toString() it:

template.execute("jvmInfo", new CollectionCallback<String>() {   String doInCollection(DBCollection collection) {     DBCursor cursor = collection.find(query)     return cursor.next().toString()   } } 

Yo'll still get the exception translation into Spring's DataAccessExceptions. Note, that this is slightly brittle as we expect only a single result to be returned for the query but that's probably something you have to take care of when trying to produce a String anyway.

2. Register a Converter from DBObject to String

You can implement a Spring Converter to do the toString() for you.

class DBObjectToStringConverter implements Converter<DBObject, String> {   public String convert(DBObject source) {     return source == null ? null : source.toString();   } } 

You can then either use the XML configuration or override customConversions() to return a new CustomConversions(Arrays.asList(new DBObjectToStringConverter())) to get it registered with your MongoConverter. You can then simply do the following:

String result = mongoTemplate.findOne(basicQuery, String.class, "jvmInfo"); 

I will add the just showed converter to Spring Data MongoDB and register it by default for the upcoming 1.3 GA release and port the fix back to 1.2.x as part of the fix for DATAMONGO-743.

like image 89
Oliver Drotbohm Avatar answered Sep 27 '22 21:09

Oliver Drotbohm