Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json Scala重头戏2:动作合成和BodyParser_Json_Scala_Playframework_Action_Composition - Fatal编程技术网

Json Scala重头戏2:动作合成和BodyParser

Json Scala重头戏2:动作合成和BodyParser,json,scala,playframework,action,composition,Json,Scala,Playframework,Action,Composition,我使用动作组合进行身份验证,并避免在每个动作中传递公共参数。我的问题是如何将它与BodyParser parse.json结合起来,就像下面的方法一样 def setUser() = Action(parse.json) { implicit request => val id = (request.body \ "id").as[String] val username = (request.body \ "username").as[String] val

我使用动作组合进行身份验证,并避免在每个动作中传递公共参数。我的问题是如何将它与BodyParser parse.json结合起来,就像下面的方法一样

def setUser() = Action(parse.json) {
  implicit request =>
    val id = (request.body \ "id").as[String]
    val username = (request.body \ "username").as[String]
    val previousURI = request.session.get("previousURI") match {
      case Some(x) => x
      case None => "/"
    }
    Ok(previousURI).withSession(
      "id" -> id,
      "username" -> username
    )
}
使用上述方法的控制器使用“Auth”特性:

case class User(id: Option[String], username: Option[String])

case class Context(user: User, request: Request[AnyContent])
  extends WrappedRequest(request)

trait Auth {

  def CheckLogin(f: Context => Result) = {

    Action { request =>
      request.session.get("username").map { token =>
        // username? yes continue...
        val id = request.session.get("id")
        val username = request.session.get("username")
        f(Context(new User(id, username), request))
      }.getOrElse {
        // no redirect to login
        Results.Redirect(routes.Application.login).withSession(
          "previousURI" -> request.uri
        )
      }

    }

  }

}
如果我尝试:

def myMethod()=CheckLogin(parse.json){

我得到:

type mismatch; found : play.api.mvc.BodyParser[play.api.libs.json.JsValue] required: controllers.Context => play.api.mvc.Result

谢谢!

回答我自己的问题,仍然不确定这是否是最优雅的,但它确实有效:

def CheckLogin(bp: BodyParser[AnyContent] = parse.anyContent)(f: Context => Result) = {
    Action(bp) { request =>
    // ...
CheckLogin
现在有一个BodyParser,默认为
parse.anyContent
,因此它可以接受其他BodyParser,以及
parse.json