我已经开始使用Play和Play-ReactiveMongo插件,并在 GET "document by id" 场景中测试 404 响应。不幸的是,我得到了这个异常,而不是 Play 返回 404 NotFound 响应:
java.util.NoSuchElementException: JsError.get
at play.api.libs.json.JsError.get(JsResult.scala:11) ~[play_2.10.jar:2.1.1]
at play.api.libs.json.JsError.get(JsResult.scala:10) ~[play_2.10.jar:2.1.1]
at play.modules.reactivemongo.json.collection.JSONGenericHandlers$StructureBufferWriter$.write(jsoncollection.scala:44) ~[play2-reactivemongo_2.10-0.9.jar:0.9]
at play.modules.reactivemongo.json.collection.JSONGenericHandlers$StructureBufferWriter$.write(jsoncollection.scala:42) ~[play2-reactivemongo_2.10-0.9.jar:0.9]
at reactivemongo.api.collections.GenericQueryBuilder$class.reactivemongo$api$collections$GenericQueryBuilder$$write(genericcollection.scala:323) ~[reactivemongo_2.10-0.9.jar:0.9]
at reactivemongo.api.collections.GenericQueryBuilder$class.cursor(genericcollection.scala:333) ~[reactivemongo_2.10-0.9.jar:0.9]
如果 id 参数与现有文档匹配,则下面的 getById 函数成功返回单个文档,但如果找不到文档,则“one [JsValue]”行上的异常。
路线文件:
GET /items/:id controllers.ItemsController.getById(id: String)
控制器对象:
object ItemsController extends Controller with MongoController {
def itemsCollection: JSONCollection = db.collection[JSONCollection]("items")
def getById(id: String) = Action {
Async {
val query = Json.obj("_id" -> Json.obj("$oid" ->id))
val futureItem = itemsCollection.
find(query).
one[JsValue]
futureItem.map {
case Some(item) => Ok(item)
case None => NotFound(Json.obj("message" -> "No such item"))
}
}
}
}
也许我错过了文档中的某些内容?
这里记录了部分示例:
https://github.com/sgodbillon/reactivemongo-demo-app#simple-query
mandubian Coast-to-coast 示例也处理 BadRequest 以及 NotFound 场景,但代码可能已过时,因为它不使用较新的 find(...).one[...] 语义?
http://mandubian.com/2013/01/13/JSON-Coast-to-Coast/#action-get