不同类型的 akka-http 完整响应

akka-http complete response with different types

如何根据未来的响应完成不同类型的 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 ]

您可以使用 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")
    }
  }
}