Scala akka dsl路由指令中的类型不匹配错误
我正在用akka http设置一个rest控制器。控制器解析url,提取变量,然后调用服务,该服务向参与者发送消息,参与者随后查询存储库并将数据作为消息发送。我最终让参与者接收消息并查询回购协议(在不得不链接一系列期货之后),但现在控制器中出现了一个我无法理解的错误:Scala akka dsl路由指令中的类型不匹配错误,scala,future,actor,akka-http,Scala,Future,Actor,Akka Http,我正在用akka http设置一个rest控制器。控制器解析url,提取变量,然后调用服务,该服务向参与者发送消息,参与者随后查询存储库并将数据作为消息发送。我最终让参与者接收消息并查询回购协议(在不得不链接一系列期货之后),但现在控制器中出现了一个我无法理解的错误: Error:(58, 41) type mismatch; found : Unit required: akka.http.scaladsl.server.RequestContext => scala.conc
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 =>
这是否意味着我必须在其他地方包含完整的()
我尝试确保参与者发送一个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"
这是rest控制器:
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))
}
}
}
}
}
val进程路由=
路径(“进程”/段){process=>
withoutRequestTimeout{
参数(“用户名”、“限制”){(推特用户名、限制)=>
得到{
val processRequest:processRequest=新的processRequest(twitterUserName、process、limit.toInt)
导入JsonSupport_
过程匹配{
案例“shout”=>
val servicesult//以后添加更多案例或动态调用方法
=processService.shout(processRequest)
var listOfTweetTexts:List[String]=List[String]()
服务结果完成{
案例成功(结果)=>
为了(推特)
actorSystem.log.error(“发生错误:”+t.getMessage)
完成(HttpEntity(ContentTypes.`text/html(UTF-8)`,“向失败问好”))
}
//完成(HttpEntity(ContentTypes.`text/html(UTF-8)`,“向”+limit打招呼))
大小写\=>complete(HttpEntity(ContentTypes.`text/html(UTF-8)`,“向”+limit打招呼))
}
完成(HttpEntity(ContentTypes.`text/html(UTF-8)`,“向”+limit打招呼))
}
}
}
}
}
您在未来
上调用onComplete
,返回单位
。您要做的是在未来
上使用AkkaonComplete
应该如此
onComplete(serviceResult) {
而不是
serviceResult onComplete {
你能修复缩进吗?这段代码中的嵌套真的很难计算出来!很抱歉,现在已经编辑好了。你确定吗?Tim?我看到的示例中有一个完整的被返回到路由中的onComplete:这是由Akka Http提供的一个不同的
onComplete
。你正在调用onComplete
ode>,它肯定返回Unit
。现在我有以下错误:error:(53,41)类型不匹配;find:Unit required:akka.http.scaladsl.server.RequestContext=>scala.concurrent.Future[akka.http.scaladsl.server.RouteResult]路径(“进程”/分段){process=>