如何根据未来的响应完成不同类型的akka-http响应? 我正在尝试做类似
的事情implicit val textFormat = jsonFormat1(Text.apply)
implicit val userFormat = jsonFormat2(User.apply)
path(Segment) { id =>
get {
complete {
(fooActor ? GetUser(id)).map {
case n: NotFound => Text("Not Found")
case u: User => u
}
}
}
}
但我正在
type mismatch , expected: ToResponseMarshable , actual Futue[Product with Serializable ]
答案 0 :(得分:2)
您可以使用onComplete
指令:
get {
path(Segment) { id =>
onComplete(fooActor ? GetUser(id)) {
case Success(actorResponse) => actorResponse match {
case _ : NotFound => complete(StatusCodes.NotFound,"Not Found")
case u : User => complete(StatusCodes.OK, u)
}
case Failure(ex) => complete(StatusCodes.InternalServerError, "bad actor")
}
}
}