【发布时间】:2019-08-21 14:03:18
【问题描述】:
我创建了一个路由以流式传输 JSON 中的案例类列表。但如果我使用 ByteString,则会打印案例类而不是 JSON
def streamRoute: Route = pathEndOrSingleSlash {
val byteString = new LocalFileParser(config).importFromFiles.map(phoneNumber => ByteString(phoneNumber.toString + "\n"))
complete(HttpEntity(ContentTypes.`application/json`, byteString))
}
// RESULT: PhoneNumber(+35799000123,Some(357),Some(Cyprus),Some(Cytamobile-Vodafone),Some(MOBILE))
如果我只使用complete(new LocalFileParser(config).importFromFiles),那么这给了我 JSON。第二种方法对流式分块响应有好处吗?如果不是,我该如何修复第一种方法以返回 JSON 而不是案例类
【问题讨论】:
-
为什么不能将案例类转换为 JSON?
标签: json scala akka-stream akka-http