1

我正在使用響應式mongo驅動程序的play-framework。對於我們的路線文件處理反應蒙戈BSONObjectId,我創建了以下基料:Play-Framework:無法將參數companyId解析爲BSONObjectID:錯誤ObjectId

object StringToBSONObjectIdBinder { 

/* This is for Path Parameter*/ 

implicit object pathBindableBSONObjectID extends play.api.mvc.PathBindable.Parsing[BSONObjectID](
    BSONObjectID(_), _.stringify, 
    (key: String, e: Exception) => 
    "Cannot parse parameter %s as BSONObjectID: %s".format(key, e.getMessage)) 

/* This is for query String*/ 

implicit object queryStringBindableBSONObjectID extends play.api.mvc.QueryStringBindable.Parsing[BSONObjectID](
    BSONObjectID(_), _.stringify, 
    (key: String, e: Exception) => 
    "Cannot parse parameter %s as BSONObjectID: %s".format(key, e.getMessage)) 
} 

在路由我是很容易的路線我的ID作爲路徑PARAM像下面的例子:

GET  /company/:companyId/users-detail  controllers.CompanyController.userDetail(companyId: BSONObjectID) 

BSONObjectId容易映射與我的請求處理程序路徑參數。但是,當我使用以下路線如下以上路線後:

GET /company/detail  controllers.CompanyController.companyDetail 

我得到以下BadRequest

For request 'GET /company/detail?t=1466673779753' [Cannot parse parameter companyId as BSONObjectID: wrong ObjectId: 'teams'] 

但是當我切換路線如下:

GET /company/detail  controllers.CompanyController.companyDetail 
GET  /company/:companyId/users-detail  controllers.CompanyController.userDetail(companyId: BSONObjectID) 

服務成功運行。我仍然沒有得到什麼實際問題。這是玩框架問題,或者我的代碼有問題?

回答

2

首先你重新實現一個QueryBindable,而BSON一個已經被播放插件提供:see sample

然後傳遞價值"teams",這是不是BSONObjectID的有效表示。