Play Framework - 尝试映射Promise

时间:2015-07-20 07:42:23

标签: java scala playframework-2.0

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;
                    }
                }   
    ); 

这会显示错误“scala.concurrent.ExecutionContext类型无法解析。它是从所需的.class文件间接引用的。”

我尝试过添加

import scala.concurrent.ExecutionContext;

然后错误只是从承诺所在的行“移动”到文件的顶部但仍然无法编译。

我也尝试过添加

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

4 个答案:

答案 0 :(得分:8)

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

播放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&gt;右键单击your_project&gt;刷新

答案 1 :(得分:1)

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

答案 2 :(得分:0)

解决方案:

import scala.concurrent.ExecutionContext;

忽略日食错误。

答案 3 :(得分:0)

还尝试从源目录(.project,.classpath等)中删除eclipse东西并重新导入项目。