Play Framework 2 Scala WS执行同步请求 [英] Play Framework 2 Scala WS Doing a Sync Request
问题描述
我正在学习Scala。我曾经使用过Play Framework 2的Java,并试图重写一些我以前的工作使用和学习Scala。
I am learning Scala. I used to use Play Framework 2 Java and trying to rewrite some of my previous work using and learning Scala.
我需要做一个同步WS请求并获得结果对象
I need to do a sync WS request and get Result Object from it somewhere in my code.
当我回到Java时,我习惯这样做:
While I was back in Java, I used to do it like this:
WS.url("someurl").get().get(5000);
或 T Promise< T> code>。
or with T Promise<T>.get(Long timeout)
to be exact.
自从我切换到Scala之后,我现在使用 play.api.libs.ws $我重写了代码:
Since I switched to Scala, I am now using play.api.libs.ws
and I rewrote code as:
val somefuture:Future[Response] = WS.url("someurl").get();
但我无法从未来获得响应 ] 同步!在scala上没有 .get()
方法。
But I can't get Response from Future[Response] syncly! There is no .get()
method on scala.
如何获得 c>
推荐答案
使用 Await.result
。
import scala.concurrent.duration._
import scala.concurrent.Await
....
val future: Future[Response] = ...
Await.result(future, 10 seconds): Response
这篇关于Play Framework 2 Scala WS执行同步请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!