我打电话这样的Web服务:
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._
由于播放2.4你可以注入默认execution context
通过Guice的依赖:注入。
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'")
}
}
}
另一种选择:
import scala.concurrent.ExecutionContext.Implicits.global