I am facing issues in retrieving JSON response from mongo db using spring data mongo template. Instead of retrieving the object id of the document, the response is coming with timestamp. Following is the JSON payload stored in mongo db:
{
"_id" : ObjectId("5457d80a59b6460a50f6cef1"),
"menuId" : 123,
"menuItemId" : 723,
"lastUpdatedDate" : "2014-10-25T20:10:10+0000",
"menuItemJson" : {
....
}
}
The following is my json response coming from 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 following is my Java POJO class which is 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 is my dao method to retrieve the 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));
return mongoMenuOrderingTemplate.findOne(query, MenuItemJsonCollection.class, "MenuDb");
}
}
And following is the rest endpoint method in the 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) {
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
return new ResponseEntity<>(menuItem, HttpStatus.OK);
}
}
}
What should i do to print only id rather than sending the response with complete timestamp ?
i.e "id" :"5457d80a59b6460a50f6cef1" rather than whole timestamp.
Thank you in advance.