我正在使用反应性蒙哥驱动程序的播放框架。为了在我们的路线文件中处理反应性mongo BSONObjectId
,我正在创建以下粘合剂:
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作为路径参数,如以下示例:
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)
服务成功运行。我仍然没有得到什么实际问题。这是播放框架问题,或我的代码有问题?
首先,您重新实现了 QueryBindable
,而bson的播放插件已经提供了:请参阅sample
然后您传递值"teams"
,这不是BSONObjectID
的有效表示。