Play Framework - 尝试映射 Promise 时无法解析 ExecutionContext

Play Framework - ExecutionContext cannot be resolved when trying to map a Promise

WS.url("https://api.humanapi.co/v1/human"+url+"?updated_since="+updatedSince).setHeader("Authorization", "Bearer "+accessToken)
        .setHeader("Accept", "application/json").get().map(
                new Function<WSResponse, JsonNode>() {
                    public JsonNode apply(WSResponse response) {
                        JsonNode json = response.asJson();
                        success(json);
                        return json;
                    }
                }   
    ); 

这会显示错误 "The type scala.concurrent.ExecutionContext cannot be resolved. It is indirectly referenced from required .class files"。

我试过添加

import scala.concurrent.ExecutionContext;

但是错误只是 "moves" 从 promise 到文件顶部的行,仍然无法编译。

我也试过添加

import play.api.libs.concurrent.Execution.Implicit.defaultContext;

但是没有导入这个东西

使用的 Play Framework 是 2.4.2。

SBT 文件:

version := "1.0-SNAPSHOT"

lazy val root = (project in file(".")).enablePlugins(PlayJava)

scalaVersion := "2.11.6"

resolvers ++= Seq(
    "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/",
    "sonatype snapshots" at "https://oss.sonatype.org/content/repositories/releases/"
)

checksums := Nil

libraryDependencies ++= Seq(
  javaJdbc,
  cache,
  javaWs,
  "org.mockito" % "mockito-all" % "1.10.19",
  "commons-codec" % "commons-codec" % "1.10",
  "de.flapdoodle.embed" % "de.flapdoodle.embed.mongo" % "1.48.0",
  "org.mongodb.morphia" % "morphia" % "1.0.0-rc0"
)

libraryDependencies += "org.mongodb" % "mongodb-driver" % "3.0.2"

// Play provides two styles of routers, one expects its actions to be injected, the
// other, legacy style, accesses its actions statically.
routesGenerator := InjectedRoutesGenerator

解决方案:

进口scala.concurrent.ExecutionContext;

忽略 eclipse 错误。

我遇到了同样的问题并意识到我的 Play Eclipse 设置不完整(我错过了下面的步骤 2 和 3)。我重试了设置,这次遵循了所有相关步骤。然后 ExecutionContext 变得可解析。

Play Eclipse 安装说明

原文参考:https://www.playframework.com/documentation/2.4.x/IDE

更新参考:https://www.playframework.com/documentation/2.5.x/IDE

1) 将此行附加到 project/plugins.sbt:

addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "4.0.0")

2) 将此行附加到 build.sbt:

EclipseKeys.preTasks := Seq(compile in Compile)

3) 将这些行附加到 build.sbt(假设您没有 Scala 源代码):

EclipseKeys.projectFlavor := EclipseProjectFlavor.Java           // Java project. Don't expect Scala IDE
EclipseKeys.createSrc := EclipseCreateSrc.ValueSet(EclipseCreateSrc.ManagedClasses, EclipseCreateSrc.ManagedResources)  // Use .class files instead of generated .scala files for views and routes 

4) 保存所有项目文件。关闭 Eclipse。

5) 在命令提示符下,cd 到您的项目文件夹和 运行:

activator "eclipse with-source=true"

6) 打开 Eclipse。打开你的项目。刷新如下:

Package Explorer > 右键单击​​ your_project > 刷新

只需从 Eclipse Marketplace 安装 ScalaIDE 插件。这将解决问题 + 允许您开发 Scala。

也尝试从源目录(.project、.classpath 等)中删除 eclipse 内容并重新导入项目。