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:
Map
objectMap
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?
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.
Anything you can represent in JSON can be natively stored in MongoDB, and retrieved just as easily in JSON.
There's two way's you can do this right now:
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With