Finch Hello World 错误:Http 不是 com.twitter.finagle 的成员



我正在尝试使用scala finch库来构建API。

我有以下简单的代码:

package example
import io.finch._
import com.twitter.finagle.Http
object HelloWorld extends App {
val api: Endpoint[String] = get("hello") { Ok("Hello, World!") }
Http.serve(":8080", api.toService)
}

还有一个看起来像这样的 build.sbt 文件:

name := "hello-finch"
version := "1.0"
scalaVersion := "2.10.6"
mainClass in (Compile, run) := Some("example.HelloWorld")
libraryDependencies ++= Seq(
"com.github.finagle" %% "finch-core" % "0.10.0"
)
// found here: https://github.com/finagle/finch/issues/604
addCompilerPlugin(
"org.scalamacros" % "paradise" % "2.1.0" cross CrossVersion.full
)

当我编译并运行代码时,我收到以下错误消息:

object Http is not a member of package com.twitter.finagle
[error] import com.twitter.finagle.Http
[error]        ^
[error] /Users/jamesk/Code/hello_finch/hello-finch/src/main/scala/example/Hello.scala:8: wrong number of type arguments for io.finch.Endpoint, should be 2
[error]   val api: Endpoint[String] = get("hello") { Ok("Hello, World!") }
[error]            ^
[error] /Users/jamesk/Code/hello_finch/hello-finch/src/main/scala/example/Hello.scala:8: not found: value get
[error]   val api: Endpoint[String] = get("hello") { Ok("Hello, World!") }
[error]                               ^
[error] /Users/jamesk/Code/hello_finch/hello-finch/src/main/scala/example/Hello.scala:10: not found: value Http
[error]   Http.serve(":8080", api.toService)
[error]   ^
[error] four errors found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 1 s, completed Aug 15, 2017 12:56:01 PM

在这一点上,我已经没有想法了,它看起来像一个不错的库,但让它工作很痛苦。任何帮助将不胜感激。

我已经更新了您的示例以使用Finch的最新版本:"com.github.finagle" %% "finch-core" % "0.15.1",并且还Scala 2.12

build.sbt 文件:

name := "hello-finch"
version := "1.0"
scalaVersion := "2.12.2"
mainClass in (Compile, run) := Some("example.HelloWorld")
libraryDependencies ++= Seq(
"com.github.finagle" %% "finch-core" % "0.15.1"
)

然后,src/main/scala/example/HelloWorld.scala文件:

package example
import io.finch._
import com.twitter.finagle.Http
import com.twitter.util.Await
object HelloWorld extends App {
val api: Endpoint[String] = get("hello") { Ok("Hello, World!") }
Await.ready(Http.server.serve(":8080", api.toServiceAs[Text.Plain]))
}

另请注意,必须Await.ready()- 否则您的程序将立即退出。

相关内容

最新更新