Play 框架:链式 ActionsBuilder 和 ActionRefiner

Play Framework: Chain ActionsBuilder and ActionRefiner

我很绝望。 我试着像官方文档的最后一段那样做 ActionComposition:https://playframework.com/documentation/2.3.x/ScalaActionsComposition

我的代码:

object ActionBuilder1 extends ActionRefiner[Request, Request] {
  override protected def refine[A](request: Request[A]): Future[Either[Result, Request[A]]] = Future {Right(request)}
}


object ActionBuilder2 extends ActionBuilder[Request] {
  def invokeBlock[A](request: Request[A], block: (Request[A]) => Future[Result]) : Future[Result] = {
    block(request)
  }
}

在我的控制器中:

def yolo = ActionBuilder2 andThen ActionBuilder1 {
  Ok("ASd")
}

但是编译器说:

actions.ActionBuilder1.type does not take parameters
def yolo = ActionBuilder2 andThen ActionBuilder1 {
                                               ^

真不知道为什么...

我认为 Scala 无法理解您的意思:

ActionBuilder2 andThen ActionBuilder1 { // Some block }

所以最简单的方法似乎是将该链声明为独立的事物,然后对其应用区块:

val actionChain = ActionBuilder2 andThen ActionBuilder1

def yolo = actionChain { 
  Ok("yolo")
}

验证 它是否按所需顺序工作(2 然后 1),通过日志记录:

object ActionBuilder1 extends ActionRefiner[Request, Request] {
  override protected def refine[A](request: Request[A]): Future[Either[Result, Request[A]]] = Future {
    Logger.info("ActionBuilder1")
    Right(request)
  } 
} 


object ActionBuilder2 extends ActionBuilder[Request] {
  def invokeBlock[A](request: Request[A], block: (Request[A]) => Future[Result]) : Future[Result] = {
    Logger.info("ActionBuilder2")
    block(request)
  }
}

在控制台中请求端点:

[info] application - ActionBuilder2
[info] application - ActionBuilder1