无法导入DefaultHttpFilters

时间:2016-08-25 21:32:06

标签: scala playframework

我正在尝试创建一个过滤器,以便在Scala Play 2.5应用中收集与请求实现时间相关的指标。我正在关注this documentation

它指示我创建一个扩展DefaultHttpFilters的类。但是,我无法导入此课程! import play.api.http.DefaultHttpFilters无法识别。在我看来,我可能需要添加build.sbt,因此我在该文件中添加了filterslibraryDependencies,但仍然没有运气。真正奇怪的是import play.api.http.HttpFilters被认可了。 DefaultHttpFilters生活在同一个包中,实际上实现了HttpFilters特征,所以我很尴尬地认为导入是无法识别的。

非常感谢任何建议,如果我能提供任何进一步的信息来帮助诊断问题,请告诉我。

这是我的build.sbt

name := """REDACTED"""

version := "1.0.0"

lazy val root = (project in file(".")).enablePlugins(PlayScala)

scalaVersion := "2.11.7"

routesGenerator := InjectedRoutesGenerator

resolvers += "scalaz-bintray" at "https://dl.bintray.com/scalaz/releases"

libraryDependencies ++= Seq(
  ws,
  filters,
  "com.typesafe.play" %% "play-slick" % "2.0.0",
  "com.h2database" % "h2" % "1.4.187",
  "org.scalatestplus.play" %% "scalatestplus-play" % "1.5.0" % "test",
  "mysql" % "mysql-connector-java" % "5.1.39",
  specs2 % Test
)

unmanagedJars in Compile += file(Path.userHome+"/lib/*.jar")

resolvers += "Sonatype snapshots" at "http://oss.sonatype.org/content/repositories/snapshots/"

fork in run := true

以下是plugins.sbt

resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"

addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.5.0")

addSbtPlugin("com.jamesward" %% "play-auto-refresh" % "0.0.14")

addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.3.5")

过滤器按以下方式定义 app/filters/AccessLoggingFilter.scala

import javax.inject.Inject

import akka.stream.Materializer
import play.api.Logger
import play.api.mvc.{Filter, RequestHeader, Result}
import play.api.routing.Router.Tags

import scala.concurrent.Future

class AccessLoggingFilter @Inject() (implicit val mat: Materializer) extends Filter {

  def apply(nextFilter: RequestHeader => Future[Result])(requestHeader: RequestHeader): Future[Result] = {
    val requestStartTime = System.nanoTime

    nextFilter(requestHeader).map { result =>
      val requestedAction = requestHeader.tags(Tags.RouteController) + "." + requestHeader.tags(Tags.RouteActionMethod)
      val requestFulfillmentTime = System.nanoTime - requestStartTime

      Logger.info("Request for " + requestedAction + " resulted in status code " + result.header.status +
        " and had request fulfillment time " + requestFulfillmentTime + " nanoseconds")

      result.withHeaders("Request-Time" -> requestFulfillmentTime.toString)
    }
  }
}

然后app/filters/Filters.scala

package filters

import javax.inject.Inject

class Filters @Inject() (accessLoggingFilter: AccessLoggingFilter) { }

1 个答案:

答案 0 :(得分:1)

DefaultHttpFilters仅在Play 2.5.4中引入,您使用的是Play 2.5.0。

因此,至少将Play版本更改为2.5.4(编写本文时的当前版本为2.5.6)

// In plugins.sbt
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.5.4")

然后,只需重新加载项目并更新您的依赖项(activator update

如果您确实需要使用该版本,请使用HttpFilters代替("使用过滤器"中的相同示例)

import javax.inject.Inject
import play.api.http.HttpFilters
import play.filters.gzip.GzipFilter

class Filters @Inject() (
  gzip: GzipFilter,
  log: LoggingFilter
) extends HttpFilters {

   val filters = Seq(gzip, log)
}