【发布时间】:2019-09-29 05:44:42
【问题描述】:
我正在使用 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() ?
我尝试确保参与者发送一个未来作为其消息的内容,并且该服务将一个未来返回给控制器,因为我认为这是避免空指针的唯一方法。
这些是我的依赖项:
"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))
}
}
}
}
}
【问题讨论】:
-
你能修复缩进吗?这段代码中的嵌套真的很难解决!
-
抱歉,现在已经修改了
标签: scala future actor akka-http