2015-10-20 41 views
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) 

回答

3

它只是說,如果你想構建Headers您需要提供一個名爲headers場是Seq[(String, String)]類型。如果你省略了inital new,你將會使用相應objectapply函數Headers,它將只是一個可變參數(String,String),你的代碼應該可以工作。如果您查看文檔https://www.playframework.com/documentation/2.4.x/api/scala/index.html#play.api.mvc.Headers並在對象和類的文檔之間切換,它應該變得清晰。

+0

感謝您的回答,我通過您的協助更新了代碼,但不確定它是否好。 – Chk0nDanger

+0

'val newHeaders = Headers((AccessLogger.X_HEADER__REQUEST_ID - > requestId)++ requestHeaders.headers.headers)'應該足夠了。 – Markus

相關問題