Finch Hello World Error: Http not a member of com.twitter.finagle

Finch Hello World Error: Http not a member of 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() 是强制性的 - 否则您的程序将立即退出。