我有一个演员接收两种类型的响应,都是json格式:Token
和ShowInfoResponse
。
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
为什么会这样?