本文介绍了播放框架:Chain ActionsBuilder和ActionRefiner的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很绝望.我尝试像在官方文档的最后一段中那样进行ActionComposition: https://playframework.com /documentation/2.3.x/ScalaActionsComposition

I am desperate.I tried to do ActionComposition like in the very last paragraph of the official docs: 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 {
                                               ^

我真的不知道为什么...

I really do not know why...

推荐答案

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

I think Scala can't work out what you mean by:

ActionBuilder2 andThen ActionBuilder1 { // Some block }

所以最简单的方法似乎是将链条本身声明为事物,然后然后在其上应用块:

so the easiest way seems to be declaring that chain as a thing in its own right, then applying the block to it:

val actionChain = ActionBuilder2 andThen ActionBuilder1

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

通过日志记录,

验证以期望的顺序运行(2然后是1):

Verification that it's working in the desired order (2 then 1), via logging:

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)
  }
}

在控制台中,在请求端点之后:

In the console upon requesting the endpoint:

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

这篇关于播放框架:Chain ActionsBuilder和ActionRefiner的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 10:34