我在探索 Play Framework 时遇到了一些困难。
在使用 WS 库对 Google Analytics 进行 API 调用后,我收到一个 Future[Response]
对象。在消化了 Response 对象之后,我得到了我真正关心的数据,但是因为它被包装在 Future 中,所以我在将它写入浏览器时遇到了一些麻烦。
OK( gaApiString )
这给我一个错误:
Cannot write an instance of scala.concurrent.Future[String] to HTTP response. Try to define a Writeable[scala.concurrent.Future[String]]
我在查找和理解如何使用 Writable
对象时遇到了一些麻烦。帮助不大?
最佳答案
您需要将 Future
map
到 Future[Result]
,并将其传递给 Action.async
.
def test = Action.async {
val wsResult: Future[String] = ...
wsResult.map { gaApiString =>
Ok(gaApiString)
}
}
如果 gaApiString
实际上是 List[String]
,那么这取决于您要用它做什么。将其显示为逗号分隔列表,您只需将其更改为 Ok(gaApiString.mkString(","))
。这里的关键是映射 Future
以在完成后操作该值。
关于scala - Play Framework : how to display a Future[List[String]]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25814035/