1

我有一个返回字符串的 Akka HTTP 服务,如下所示:

val route1: Route = {
    path("hello") {
      get{
        complete{
          println("Inside r1")
          "You just accessed hello"
        }
      }
   }
}

我有一个 Akka HTTP 客户端,它试图访问这个路由。但是下面的代码失败了:

  val future1 = Http()
    .singleRequest(
      HttpRequest(method = HttpMethods.GET,
        uri = "http://localhost:8187/hello")).mapTo[String]

  future1.onSuccess({
    case y:String=>println(y)
  })

我根本没有输出。但是,如果我将 unmarshal 与 flatMap 一起使用,我会得到输出:

 val future1:Future[String] = Http()
    .singleRequest(
      HttpRequest(method = HttpMethods.GET,
                  uri = "http://localhost:8187/hello")).flatMap(resp => Unmarshal(resp).to[String])

为什么 mapTo 在这里失败,为什么我需要 flatMap 和 Unmarshal?

编辑:

我了解 Unmarhsal 的必要性,我正在尝试了解 map 和 flatMap 之间的区别

例如,下面的代码给了我预期的结果:

val future1:Future[String] = Http().singleRequest(
          HttpRequest(method = HttpMethods.GET,
                      uri = http://localhost:8187/hello")).flatMap(testFlatFunc)

  def testFlatFunc(x:HttpResponse):Future[String]={
    return Unmarshal(x).to[String]
  }

但是,如果我尝试用地图替换它,如下所示,我得到的输出为FulfilledFuture(You just accessed hello)

 val future1:Future[String] = Http()
    .singleRequest(
      HttpRequest(method = HttpMethods.GET,
                  uri = "http://localhost:8187/hello")).map(testFunc)

  def testFunc(x:HttpResponse): String={
    return Unmarshal(x).to[String].toString
  }
4

1 回答 1

4

请参阅mapTo下面的文档

  /** Creates a new `Future[S]` which is completed with this `Future`'s result if
   *  that conforms to `S`'s erased type or a `ClassCastException` otherwise.
   */

mapTo[S]基本上对应于演员表。Http().singleRequest产生 a Future[HttpResponse],并且不能HttpResponse直截了当地转换为String

Umarshalling 是必要的,以指定一个有意义的逻辑来转换为String. 因此,在您的情况下,您有一个隐含Unmarshaller的范围来提供此功能。这很可能是stringUnmarshallerAkka-HTTP 预定义集中的默认值。可以在文档中找到更多信息。

于 2017-02-17T09:50:52.710 回答