[英]playframework 2.4 - Unspecified value parameter headers error
I am upgrading playframework 2.4 from 2.3, I changed versions then if I compile same code, I see following error. 我正在从2.3升级playframework 2.4,我更改了版本然后如果我编译相同的代码,我看到以下错误。 Since I am novice at Scala, I am trying to learn Scala to solve this issue but still don't know what is the problem.
由于我是Scala的新手,我正在尝试学习Scala来解决这个问题,但仍然不知道是什么问题。 What I want to do is adding a request header value from original request headers.
我想要做的是从原始请求标头添加请求标头值。 Any help will be appreciated.
任何帮助将不胜感激。
[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
The LoggingFilter class 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;
}
}
}
Edit 编辑
I updated codes as following and it compiled well 我更新了代码如下,编译得很好
following codes changed 以下代码已更改
val newHeaders = new Headers { val data = (requestHeader.headers.toMap
+ (AccessLogger.X_HEADER__REQUEST_ID -> Seq(requestId))).toList }
to 至
val newHeaders = new Headers((requestHeader.headers.toSimpleMap
+ (AccessLogger.X_HEADER__REQUEST_ID -> requestId)).toList)
It simply states that if you want to construct Headers
you need to supply a field named headers
which is of type Seq[(String, String)]
. 它只是声明如果你想构造
Headers
你需要提供一个名为headers
的字段,其类型为Seq[(String, String)]
。 If you omit the inital new
you will be using the apply
function of the corresponding object
for Headers
which will just take a parameter of a vararg of (String, String) and your code should work. 如果省略inital
new
,则将使用Headers
的相应object
的apply
函数,它只接受vararg(String,String)的参数,并且您的代码应该有效。 If you look at documentation https://www.playframework.com/documentation/2.4.x/api/scala/index.html#play.api.mvc.Headers and flip between the docs for object and class it should become clear. 如果您查看文档https://www.playframework.com/documentation/2.4.x/api/scala/index.html#play.api.mvc.Headers并在对象和类的文档之间进行切换,它应该变得清晰。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.