4

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.

2
  • What are you using to "print" the response? Please share the code you are using to find and print the result. Commented Nov 8, 2014 at 12:33
  • @helmy i've added the controller method as well as dao method to retrieve the document and send it as response entity Commented Nov 8, 2014 at 12:50

1 Answer 1

4

The issue here is with the Jackson serialization of the JSON response. These threads demonstrate how to wire up a custom serializer:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.