我期待请求正文中的JsObject,我想提取并验证必填字段
请求正文:
{
"protoName": "category",
"clientId": 2
}
我的阅读:
val userReads: Reads[JsObject] = (
(__ \ "protoName").read[String] ~
(__ \ "clientId").read[Int] ~
(__ \ "additionalFields").readNullable[JsObject]
)((protoName: String, clientId: Int, addfields: Option[JsObject]) => Json.obj(
"protoName" -> protoName,
"clientId" -> clientId,
"additionalFields" -> addfields
))
我的控制器:
def create = Action.async(parse.json) { r =>
r.body.transform(ModelGroup.userReads).fold(
err => Future.successful(BadRequest(JsError.toJson(err))),
g => ModelGroupsDAO.insert(g).map {
case true => Ok
case false => BadGateway
}
)
}
这段代码可以工作,但它的样板代码太多......我的意思是我的Reads。 我认为这段代码可行:
val userReads: Reads[JsObject] = (
(__ \ "protoName").read[String] ~
(__ \ "clientId").read[Int] ~
(__ \ "additionalFields").readNullable[JsObject]
)(JsObject.apply _)
但它没有编译。我认为有一个漂亮的解决方案