playframework 2.4 - 未指定的值参数头错误

时间:2015-10-20 12:49:01

标签: scala playframework playframework-2.4

我正在从2.3升级playframework 2.4,我改变版本然后如果我编译相同的代码,我看到以下错误。由于我是Scala的新手,我正在尝试学习Scala来解决这个问题,但仍然不知道问题是什么。我想要做的是从原始请求标头添加请求标头值。任何帮助将不胜感激。

[error] /mnt/garner/project/app-service/app/com/company/playframework/filters/LoggingFilter.scala:26: not enough arguments for constructor Headers: (headers: Seq[(String, String)])play.api.mvc.Headers.
[error] Unspecified value parameter headers.
[error]     val newHeaders = new Headers { val data = (requestHeader.headers.toMap

LoggingFilter类

class LoggingFilter extends Filter {

  val logger = AccessLogger.getInstance();

  def apply(next: (RequestHeader) => Future[Result])(requestHeader: RequestHeader): Future[Result] = {

    val startTime = System.currentTimeMillis

    val requestId = logger.createLog();

    val newHeaders = new Headers { val data = (requestHeader.headers.toMap
      + (AccessLogger.X_HEADER__REQUEST_ID -> Seq(requestId))).toList }
    val newRequestHeader = requestHeader.copy(headers = newHeaders)
    next(newRequestHeader).map { result =>
      val endTime = System.currentTimeMillis
      val requestTime = endTime - startTime

      val bytesToString: Enumeratee[ Array[Byte], String ] = Enumeratee.map[Array[Byte]]{ bytes => new String(bytes) }
      val consume: Iteratee[String,String] = Iteratee.consume[String]()
      val resultBody : Future[String] = result.body |>>> bytesToString &>> consume

      resultBody.map {
        body =>
          logger.finish(requestId, result.header.status, requestTime, body)
      }
      result;

    }
  }


}

修改

我更新了以下代码,编译得很好

以下代码已更改

    val newHeaders = new Headers { val data = (requestHeader.headers.toMap
      + (AccessLogger.X_HEADER__REQUEST_ID -> Seq(requestId))).toList }

    val newHeaders = new Headers((requestHeader.headers.toSimpleMap
      + (AccessLogger.X_HEADER__REQUEST_ID -> requestId)).toList)

1 个答案:

答案 0 :(得分:3)

它只是说明如果你想构建Headers,你需要提供一个名为headers的字段Seq[(String, String)]。如果省略初始new,您将使用相应apply的{​​{1}}函数object,它只会获取vararg(String,String)的参数你的代码应该有效。如果您查看文档https://www.playframework.com/documentation/2.4.x/api/scala/index.html#play.api.mvc.Headers并在对象和类的文档之间进行切换,那么它应该变得清晰。