0

我正在尝试使用 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

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

4

1 回答 1

2

我已经更新了您的示例以使用 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()是强制性的 - 否则您的程序将立即退出。

于 2017-08-15T13:37:06.107 回答