我在Scala中编写了一个Play 2.3.2应用程序。 我正在编写一个Statistic控制器来查询我的mongodb数据库以获取一些信息。 现在我正在尝试实现一个方法,该方法返回与用户关联的所有标记。 我得到一个json格式的http请求,如下所示:
{
"user": "example@example.com"
}
我想解析Json请求并将String关联到Json的“user”字段,如果Json正确我想要对String对象做一些工作,否则我想返回BadRequest响应。
我的方法实现是这样的:
def userEmail = Action.async { request =>
val userEmail: String = request.body.asJson.map { json =>
json.validate[String].map {
//if the json congaing the "user tag" return the instance, get the instance, otherwise return a BadRequestInstance
}
}
def elaborate(user: String)= {
//some work
}
elaborate(userEmail)
}
我该怎么做?
答案 0 :(得分:1)
正如johanandren提到的阅读playframework文档应解决您的问题。
提示:我将定义case类和隐式读取以将json数据转换为case类类型。
case class User(email: String)
implicit val userReads = (
(JsPath \ "email").read[String]
)(User.apply _)