0

我期待 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
      }
    )

  }

这段代码有效,但它的样板代码太多......我的意思是我的阅读。我认为这段代码会起作用:

  val userReads: Reads[JsObject] = (
    (__ \ "protoName").read[String] ~
    (__ \ "clientId").read[Int] ~
    (__ \ "additionalFields").readNullable[JsObject]
  )(JsObject.apply _)

但它不编译。我认为有一个很好的解决方案

4

0 回答 0