Akka DSL路由指令中的类型不匹配错误

时间:2019-05-11 16:53:53

标签: scala future actor akka-http

我正在使用akka http设置rest控制器。控制器解析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发送一个future作为其消息的内容,并确保该服务将Future返回给控制器,因为我认为这是避免空指针的唯一方法。

这些是我的依赖项:

"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))
      }
    }
  }
}
}

1 个答案:

答案 0 :(得分:3)

您正在调用onComplete的{​​{1}}上返回Future。您要做的是使用Unit上的Akka onComplete

应该是

Future

而不是

onComplete(serviceResult) {