Play Framework 2.1 – 找不到隐式的ExecutionContext

我打电话给这样的webservice:

WS .url(url) .get .map { response => // error occurs on this line response.status match { case 200 => Right(response.json) case status => Left(s"Problem accessing api, status '$status'") } } 

完整的错误: Error: Cannot find an implicit ExecutionContext, either require one yourself or import ExecutionContext.Implicits.global

根据这个问题 ,它在文档中是固定的。 我需要添加以下导入:

 import play.api.libs.concurrent.Execution.Implicits._ 

从Play 2.4开始,你可以通过Guice dependency注入默认的execution context :injection。

 class Foo @Inject()()(implicit ec:ExecutionContext) { def bar() = { WS.url(url) .get .map { response => // error occurs on this line response.status match { case 200 => Right(response.json) case status => Left(s"Problem accessing api, status '$status'") } } } 

另一种select:

 import scala.concurrent.ExecutionContext.Implicits.global