3

spray-client用来访问 REST 服务。服务器返回的部分数据在 http 响应头中(其余在响应正文中)。

为了能够解组响应,我使用Unmarshaller. 但是,解组器只能访问响应正文(作为 的实例HttpEntity),并且在此阶段似乎无法访问所有标头。

这是当前的管道和解组器代码:

  implicit val IDsUnmarshaller = 
    Unmarshaller[List[ID]](MediaTypes.`text/plain`) {
      case HttpEntity.NonEmpty(contentType, data) => 
        data.asString.split("\n").toList.map( ID(_) )
    }

  val pipeline: HttpRequest => Future[List[ID]] = (
    encode(Gzip)
    ~> sendReceive
    ~> decode(Deflate)
    ~> unmarshal[List[ID]]
  )

解组时是否可以访问它们?有什么解决办法吗?

4

1 回答 1

1

如果您提供 FromResponseUnmarshaller 而不是普通的 Unmarshaller,您也可以访问标头。

有关创建 FromResponseUnmarshallers 的方法,请参阅此文件:https ://github.com/spray/spray/blob/master/spray-httpx/src/main/scala/spray/httpx/unmarshalling/Deserializer.scala

例如,您可以提供一个隐式函数HttpResponse => List[ID],并且应该采用该函数。

于 2015-06-06T05:58:56.630 回答