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

pdeva picture pdeva · Aug 19, 2013 · Viewed 32.2k times · Source

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?

Answer

Oliver Drotbohm picture Oliver Drotbohm · Aug 20, 2013

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.