我正在使用 Akka HTTP(在 Scala 中)开发 REST 服务。我希望将传入 http get 请求的参数转换为 ZonedDateTime 类型。如果我尝试使用 String 或 Int 但使用 ZonedDateTime 类型失败,则代码可以正常工作。代码看起来像这样:
parameters('testparam.as[ZonedDateTime])
这是我看到的错误:
Error:(23, 35) type mismatch;
found : akka.http.scaladsl.common.NameReceptacle[java.time.ZonedDateTime]
required: akka.http.scaladsl.server.directives.ParameterDirectives.ParamMagnet
parameters('testparam.as[ZonedDateTime]){
如果我向列表中添加多个参数,则会收到不同的错误:
Error:(23, 21) too many arguments for method parameters: (pdm: akka.http.scaladsl.server.directives.ParameterDirectives.ParamMagnet)pdm.Out
parameters('testparam.as[ZonedDateTime], 'testp2){
我在研究这个问题的时候在文档中发现了这个http://doc.akka.io/japi/akka-stream-and-http-experimental/2.0/akka/http/scaladsl/server/directives/ParameterDirectives.html我尝试了添加的解决方法import akka.http.scaladsl.server.directives.ParameterDirectives.ParamMagnet
以及使用 Scala 2.11 但问题仍然存在。
有人可以解释一下我做错了什么以及为什么 ZonedDateTime 类型不起作用吗?提前致谢!
这是一个代码片段,应该可以重现我所看到的问题
import java.time.ZonedDateTime
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model._
import akka.http.scaladsl.server.Directives._
import akka.stream.ActorMaterializer
import scala.io.StdIn
object WebServer {
def main(args: Array[String]) {
implicit val system = ActorSystem("my-system")
implicit val materializer = ActorMaterializer()
// needed for the future flatMap/onComplete in the end
implicit val executionContext = system.dispatcher
val route =
path("hello") {
get {
parameters('testparam.as[ZonedDateTime]){
(testparam) =>
complete(testparam.toString)
}
}
}
val bindingFuture = Http().bindAndHandle(route, "localhost", 8080)
println(s"Server online at http://localhost:8080/\nPress RETURN to stop...")
StdIn.readLine() // let it run until user presses return
bindingFuture
.flatMap(_.unbind()) // trigger unbinding from the port
.onComplete(_ => system.terminate()) // and shutdown when done
}
}