Thursday 15 September 2011

json - Spring data mongo template returning timestamp instead of plain object id -



json - Spring data mongo template returning timestamp instead of plain object id -

i facing issues in retrieving json response mongo db using spring info mongo template. instead of retrieving object id of document, response coming timestamp. next json payload stored in mongo db:

{ "_id" : objectid("5457d80a59b6460a50f6cef1"), "menuid" : 123, "menuitemid" : 723, "lastupdateddate" : "2014-10-25t20:10:10+0000", "menuitemjson" : { .... } }

the next json response coming mongo db:

{ "id": { "time": 1415043082000, "date": 1415043082000, "timesecond": 1415043082, "inc": 1358352113, "machine": 1505117706, "new": false, "timestamp": 1415043082 }, "menuid": 123, "menuitemid": 723, "lastupdateddate": "2014-10-25t20:10:10+0000", "menuitemjson": { ...... } }

and next java pojo class mapped using spring http message convertor:

import org.bson.types.objectid; import org.springframework.data.annotation.id; import org.springframework.data.mongodb.core.mapping.document; @document(collection = "menuitem") public class menuitemjsoncollection { @id private objectid id; private integer menuid; private integer menuitemid; .... }

following dao method retrieve collection:

import org.springframework.beans.factory.annotation.autowired; import org.springframework.data.mongodb.core.mongotemplate; import org.springframework.stereotype.repository; import org.springframework.data.mongodb.core.mongotemplate; import org.springframework.data.mongodb.core.query.criteria; import org.springframework.data.mongodb.core.query.query; @repository public class menudaoimpl implements menudao { @autowired private mongotemplate mongomenuorderingtemplate; @override public menuitemjsoncollection fetchmenuitembyid(long menuitemid) { query query = new query(); query.addcriteria(criteria.where("id").is(menuitemid)); homecoming mongomenuorderingtemplate.findone(query, menuitemjsoncollection.class, "menudb"); } }

and next rest endpoint method in controller:

@controller public class menuordercontroller { @autowired private menuorderservice menuorderservice; @requestmapping(method = requestmethod.get, value = "/menuitems/{id}", produces = "application/json") public responseentity<menuitemjsoncollection> getmenuitembymenu( @pathvariable("id") string menuitemid, @requestparam(value = "lastupdatedatetime", required = false) string lastupdatedatetimestring) { long menuitemid = null; try{ menuitemid = long.parselong(menuitemid); }catch(exception exe){ throw new classifiedsbadrequestexception(errormapping.invalid_payload.geterrorcode()); } if (!paramvalidationutil.validatenotnulloremptyparams(menuitemid) ) { throw new classifiedsbadrequestexception(errormapping.missing_field.geterrorcode()); } else{ menuitemjsoncollection menuitem = menuorderservice.fetchmenuitembyid(menuitemid, lastupdatedatetimestring); if (menuitem == null) { homecoming new responseentity<>(httpstatus.no_content); } homecoming new responseentity<>(menuitem, httpstatus.ok); } } }

what should print id rather sending response finish timestamp ?

i.e "id" :"5457d80a59b6460a50f6cef1" rather whole timestamp.

thank in advance.

the issue here jackson serialization of json response. these threads demonstrate how wire custom serializer:

spring info mongodb query converts string objectid automatically jackson mapper serialize/deserialize objectid

json spring mongodb nosql spring-data

No comments:

Post a Comment