1
我從2.3升級playframework 2.4,我改變了版本,然後如果我編譯相同的代碼,我看到下面的錯誤。由於我是Scala的新手,我正在嘗試學習Scala來解決這個問題,但仍然不知道問題所在。我想要做的是從原始請求標頭添加請求標頭值。任何幫助將不勝感激。playframework 2.4 - 未指定的值參數標題錯誤
[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)
感謝您的回答,我通過您的協助更新了代碼,但不確定它是否好。 – Chk0nDanger
'val newHeaders = Headers((AccessLogger.X_HEADER__REQUEST_ID - > requestId)++ requestHeaders.headers.headers)'應該足夠了。 – Markus