簡體   English   中英

Spring數據mongo模板返回時間戳而不是普通對象id

[英]Spring data mongo template returning timestamp instead of plain object id

我正面臨使用spring data mongo模板從mongo db檢索JSON響應的問題。 響應帶有時間戳,而不是檢索文檔的對象ID。 以下是存儲在mongo db中的JSON有效負載:

{
    "_id" : ObjectId("5457d80a59b6460a50f6cef1"),
    "menuId" : 123,
    "menuItemId" : 723,
    "lastUpdatedDate" : "2014-10-25T20:10:10+0000",
    "menuItemJson" : {

       ....

     }
}

以下是來自mongo db的json響應:

 {
    "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": {

                 ......

            }
     }

以下是我的Java POJO類,它使用spring http消息轉換器進行映射:

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;

        ....

      }

以下是我檢索集合的dao方法:

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");
        }
 }  

以下是控制器中的rest端點方法:

@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);

        }
    }
}

我該怎么做才能打印id而不是發送帶有完整時間戳的響應?

即“id”:“5457d80a59b6460a50f6cef1”而不是整個時間戳。

先感謝您。

這里的問題是傑克遜序列化JSON響應。 這些線程演示了如何連接自定義序列化程序:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM