Play Framework 2 Scala WS执行同步请求 [英] Play Framework 2 Scala WS Doing a Sync Request

查看:368
本文介绍了Play Framework 2 Scala WS执行同步请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆