akka-http unmarshalling与字符串一起使用但不适用于实体

时间:2017-05-08 19:40:31

标签: akka akka-http spray-json

我有一个演员接收两种类型的响应,都是json格式:TokenShowInfoResponse

case class Token(token: String)
case class GetShowInfoResponse(data: List[TvdbEpisode])
case class TvdbEpisode(episodeName: String, airedSeason: Int, airedEpisodeNumber: Int, firstAired: String)

akka-http使用Token解组实体没有问题,但由于某种原因它不会用ShowInfoResponse解组实体,即使它提取实体的正文也能正常工作:

def receive = {
  case HttpResponse(StatusCodes.OK, _, entity, _) =>
    entity.dataBytes.runFold(ByteString(""))(_ ++ _).foreach { body =>
      Unmarshal(body.utf8String).to[ShowInfoResponse] pipeTo self
      Unmarshal(body.utf8String).to[Token] pipeTo self
    }
    Unmarshal(entity).to[ShowInfoResponse] pipeTo self
    Unmarshal(entity).to[Token] pipeTo self
  case Token(_) => 
    println("Received token")
  case ShowInfoResponse(_) =>
    println("Received show info")

结果(注意缺少第二个'收到的节目信息'):

Received token
Received token
Received show info

为什么会这样?

0 个答案:

没有答案