akka-http完全响应不同类型

时间:2017-01-04 16:02:39

标签: scala akka-http

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

1 个答案:

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