在服务器上,您可能希望将应用程序部署成战争。 如何使用spray-servlet进行战争 。 在本地,最简单的方法是在没有应用程序服务器的情况下运行。
我们同时包含了Spray-Servlet和Spray-can依赖项:
name := "sprayApiExample"
version := "1.0"
scalaVersion := "2.11.6"
libraryDependencies ++= {
val akkaV = "2.3.9"
val sprayV = "1.3.3"
Seq(
"io.spray" %% "spray-can" % sprayV,
"io.spray" %% "spray-servlet" % sprayV,
"io.spray" %% "spray-routing" % sprayV,
"io.spray" %% "spray-json" % "1.3.1", //has not been updated yet
"com.typesafe.akka" %% "akka-actor" % akkaV
)
}
//This adds tomcat dependencies, you can also use jetty()
tomcat()
我们以所有常见功能为特色。 我们从中扩展SprayApiServlet和SprayApiCan。
import akka.actor.{ActorSystem, Props}
import akka.io.IO
import akka.pattern.ask
import akka.util.Timeout
import spray.can.Http
import spray.servlet.WebBoot
import scala.concurrent.duration._
trait SprayApi {
implicit val system = ActorSystem("SprayApiApp")
val apiActor = system.actorOf(Props[ApiActor], "apiActor")
}
//for use with spray-servlet
class SprayApiServlet extends WebBoot with SprayApi {
override val serviceActor = apiActor
}
//for use with spray-can
object SprayApiCan extends App with SprayApi {
implicit val timeout = Timeout(5.seconds)
IO(Http) ? Http.Bind(apiActor, interface = "localhost", port = 8080)
}
翻译自: https://www.javacodegeeks.com/2015/04/alternating-between-spray-servlet-and-spray-can.html