如何在playframework 2.4中运行httpFilters

时间:2016-03-07 19:13:55

标签: scala playframework playframework-2.4 macwire

我正在尝试使用macwire DI在Playframework 2.4上构建应用程序,我遇到来自Play的httpFilters的问题!这是我正在尝试做的一个例子

 df_qtr <- list( fmli1, fmli2, ...)

DI模块

class ExampleFilter extends Filter {
  def apply(nextFilter: RequestHeader => Future[Result])
           (requestHeader: RequestHeader): Future[Result] = {
    nextFilter(requestHeader).map { result =>
      result.withHeaders("Example" -> "test")
    }
  }
}

class Filters(ex:ExampleFilter) extends HttpFilters {
  val filters = Seq(ex)
}

Loader class

trait Module extends EhCacheComponents with BuiltInComponents {

  lazy val exampleFilter = wire[ExampleFilter]
  lazy val filters = wire[Filters]

application.conf的一部分是

class Loader extends ApplicationLoader {
  def load(context: Context) = {
    new MyComponents(context).application
  }
}

class MyComponents(context: Context) extends BuiltInComponentsFromContext(context) with Module {
  lazy val router: Router = wire[Routes] withPrefix "/"
}

和ExampleFilter不起作用。我收到没有“示例”标题的回复

2 个答案:

答案 0 :(得分:1)

看起来我找到了解决方案

class MyComponents(context: Context) extends BuiltInComponentsFromContext(context) with Module {
  override lazy val httpFilters = Seq(ExampleFilter)
  lazy val router: Router = wire[Routes] withPrefix "/"
}

,只需从 BuiltInComponents 特征覆盖 httpFilters (如上所述),然后将 ExampleFilter 更改为对象即可运行

答案 1 :(得分:0)

看起来您的Filters位于未命名(默认)的包中。把它放在包中的某个地方,比如appfilters.Filters

所以

package appfilters

...

class Filters(ex:ExampleFilter) extends HttpFilters {

...

play.http.filters = appfilters.Filters