【问题标题】:type mismatch error in akka dsl route directiveakka dsl 路由指令中的类型不匹配错误
【发布时间】: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


【解决方案1】:

您在Future 上调用onComplete,它返回Unit。您要做的是在Future 上使用Akka onComplete

应该是这样的

onComplete(serviceResult) {

而不是

serviceResult onComplete {

【讨论】:

  • 你确定蒂姆吗?我见过的例子有一个完整的被返回到一个路由中的 onComplete :doc.akka.io/docs/akka-http/current/routing-dsl/directives/…
  • 那是Akka Http提供的不同的onComplete。您在Future 上调用onComplete,它肯定会返回Unit
  • 现在我有以下错误:错误:(53、41)类型不匹配;找到:所需单元:akka.http.scaladsl.server.RequestContext => scala.concurrent.Future[akka.http.scaladsl.server.RouteResult] path("process" / Segment) { process =>
猜你喜欢
  • 2018-04-20
  • 2014-01-08
  • 2021-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多