akka dsl 路由指令中的类型不匹配错误
type mismatch error in akka dsl route directive
我正在使用 akka http 设置一个休息控制器。控制器解析 url,提取变量,然后调用服务向参与者发送消息,然后参与者查询存储库并将数据作为消息发送。我终于让演员接收消息并查询回购协议(在必须链接一系列期货之后),但现在我在控制器中出现我无法理解的错误:
Error:(58, 41) type mismatch;
found : Unit
required: akka.http.scaladsl.server.RequestContext =>
scala.concurrent.Future[akka.http.scaladsl.server.RouteResult]
path("process" / Segment) { process =>
这是否意味着我必须在其他地方包含一个 complete()?
我尝试确保 actor 将未来作为其消息的内容发送,并且服务 returns 将未来发送给控制器,因为我认为这是避免空指针的唯一方法。
这些是我的依赖项:
"com.typesafe.akka" %% "akka-http" % "10.1.8",
"com.typesafe.akka" %% "akka-actor" % "2.5.22",
"com.typesafe.akka" %% "akka-stream" % "2.5.22",
"com.typesafe.akka" %% "akka-http-spray-json" % "10.1.8"
这是其余控制器:
val processRoute =
path("process" / Segment) { process =>
withoutRequestTimeout {
parameters("userName", "limit") { (twitterUserName, limit) =>
get {
val processRequest: ProcessRequest = new ProcessRequest(twitterUserName, process, limit.toInt)
import JsonSupport._
process match {
case "shout" =>
val serviceResult // add more cases in future or call method dynamically
= processService.shout(processRequest)
var listOfTweetTexts: List[String] = List[String]()
serviceResult onComplete {
case Success(result) =>
for (tweet <- result.tweets) listOfTweetTexts ::= tweet;
complete(listOfTweetTexts)
case Failure(t) =>
actorSystem.log.error("An error has occurred: " + t.getMessage)
complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "Say hello to failure"))
}
// complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "Say hello to" + limit))
case _ => complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "Say hello to" + limit))
}
complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "Say hello to" + limit))
}
}
}
}
}
您正在 returns Unit
的 Future
上呼叫 onComplete
。你想要做的是在 Future
.
上使用 Akka onComplete
所以应该是
onComplete(serviceResult) {
而不是
serviceResult onComplete {
我正在使用 akka http 设置一个休息控制器。控制器解析 url,提取变量,然后调用服务向参与者发送消息,然后参与者查询存储库并将数据作为消息发送。我终于让演员接收消息并查询回购协议(在必须链接一系列期货之后),但现在我在控制器中出现我无法理解的错误:
Error:(58, 41) type mismatch;
found : Unit
required: akka.http.scaladsl.server.RequestContext =>
scala.concurrent.Future[akka.http.scaladsl.server.RouteResult]
path("process" / Segment) { process =>
这是否意味着我必须在其他地方包含一个 complete()?
我尝试确保 actor 将未来作为其消息的内容发送,并且服务 returns 将未来发送给控制器,因为我认为这是避免空指针的唯一方法。
这些是我的依赖项:
"com.typesafe.akka" %% "akka-http" % "10.1.8",
"com.typesafe.akka" %% "akka-actor" % "2.5.22",
"com.typesafe.akka" %% "akka-stream" % "2.5.22",
"com.typesafe.akka" %% "akka-http-spray-json" % "10.1.8"
这是其余控制器:
val processRoute =
path("process" / Segment) { process =>
withoutRequestTimeout {
parameters("userName", "limit") { (twitterUserName, limit) =>
get {
val processRequest: ProcessRequest = new ProcessRequest(twitterUserName, process, limit.toInt)
import JsonSupport._
process match {
case "shout" =>
val serviceResult // add more cases in future or call method dynamically
= processService.shout(processRequest)
var listOfTweetTexts: List[String] = List[String]()
serviceResult onComplete {
case Success(result) =>
for (tweet <- result.tweets) listOfTweetTexts ::= tweet;
complete(listOfTweetTexts)
case Failure(t) =>
actorSystem.log.error("An error has occurred: " + t.getMessage)
complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "Say hello to failure"))
}
// complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "Say hello to" + limit))
case _ => complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "Say hello to" + limit))
}
complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "Say hello to" + limit))
}
}
}
}
}
您正在 returns Unit
的 Future
上呼叫 onComplete
。你想要做的是在 Future
.
onComplete
所以应该是
onComplete(serviceResult) {
而不是
serviceResult onComplete {