Scala:使用 Spray 的多个路径前缀
Scala: multiple pathPrefixes with Spray
我正在尝试使用 Spray 创建一个 API 来侦听 2 个前缀。这 2 个前缀依次监听可选的整数。
这是我要实现的设置:
val itemRoute = {
pathPrefix("configs") {
<...>
}
pathPrefix("samples") {
<...>
}
}
这样,API就可以接听像
http://www.example.com/samples/2
这样的电话了
然而,对于所述片段,仅收听两个前缀之一。
我尝试过不同的语法风格,比如在两个 pathPrefix 块之间放置一个 ~,并合并 pathPrefixTest。这是我的语法问题吗,我怎样才能实现多个路径前缀?
使用 Akka http、Spray:
spray is no longer maintained and has been superseded by Akka HTTP.
Please check out the migration guide for help with the upgrade.
Commercial support is available from Lightbend.
无论如何,这个例子是可行的:
package test
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.Http
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
object TesHttp {
val routes = pathPrefix("configs") {
complete {
"configs"
}
} ~
pathPrefix("samples") {
complete {
"samples"
}
}
def main(args: Array[String]) : Unit = {
implicit val system = ActorSystem()
implicit val mat = ActorMaterializer()
import system.dispatcher
println("Starting ..")
val binding = Http().bindAndHandle(routes, interface = "localhost", 9091)
}
}
我正在尝试使用 Spray 创建一个 API 来侦听 2 个前缀。这 2 个前缀依次监听可选的整数。
这是我要实现的设置:
val itemRoute = {
pathPrefix("configs") {
<...>
}
pathPrefix("samples") {
<...>
}
}
这样,API就可以接听像
http://www.example.com/samples/2这样的电话了 然而,对于所述片段,仅收听两个前缀之一。
我尝试过不同的语法风格,比如在两个 pathPrefix 块之间放置一个 ~,并合并 pathPrefixTest。这是我的语法问题吗,我怎样才能实现多个路径前缀?
使用 Akka http、Spray:
spray is no longer maintained and has been superseded by Akka HTTP. Please check out the migration guide for help with the upgrade. Commercial support is available from Lightbend.
无论如何,这个例子是可行的:
package test
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.Http
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
object TesHttp {
val routes = pathPrefix("configs") {
complete {
"configs"
}
} ~
pathPrefix("samples") {
complete {
"samples"
}
}
def main(args: Array[String]) : Unit = {
implicit val system = ActorSystem()
implicit val mat = ActorMaterializer()
import system.dispatcher
println("Starting ..")
val binding = Http().bindAndHandle(routes, interface = "localhost", 9091)
}
}