Scala 为什么spray服务器不能响应http请求?

Scala 为什么spray服务器不能响应http请求?,scala,spray,Scala,Spray,我正在尝试使用设置一个非常基本的HTTP服务器。如果我调用为其设置映射的端点,我会得到一个超时,尽管使用调试器我可以看到参与者收到消息 我的消息来源如下: import akka.actor.{Actor, ActorRef, ActorSystem, Props} import akka.io.IO import spray.can.Http import spray.http.{HttpMethods, HttpRequest, HttpResponse, Uri} class App e

我正在尝试使用设置一个非常基本的HTTP服务器。如果我调用为其设置映射的端点,我会得到一个超时,尽管使用调试器我可以看到参与者收到消息

我的消息来源如下:

import akka.actor.{Actor, ActorRef, ActorSystem, Props}
import akka.io.IO
import spray.can.Http
import spray.http.{HttpMethods, HttpRequest, HttpResponse, Uri}

class App extends Actor {

  implicit val system = context.system

  override def receive = {
    case "start" =>
      val listener: ActorRef = system.actorOf(Props[HttpListener])
      IO(Http) ! Http.Bind(listener, interface = "localhost", port = 8080)
  }

}

class HttpListener extends Actor {

  def receive = {
    case _: Http.Connected =>
      sender() ! Http.Register(self)
    case HttpRequest(HttpMethods.GET, Uri.Path("/ping"), _, _, _) =>
      HttpResponse(entity = "PONG")
  }

}

object Main {

  def main(args: Array[String]) {
    val system = ActorSystem("my-actor-system")
    val app: ActorRef = system.actorOf(Props[App], "app")
    app ! "start"
  }

}
执行运行显示:

当我点击时,会显示以下HTTP/1.1500内部服务器错误http://localhost:8080/ping:

我的build.sbt看起来像:

scalaVersion := "2.11.2"

resolvers += "spray repo" at "http://repo.spray.io"

libraryDependencies ++= Seq(
  "io.spray" %% "spray-can" % "1.3.1",
  "io.spray" %% "spray-routing" % "1.3.1",
  "com.typesafe.akka" %% "akka-actor" % "2.3.5"
)
你知道我做错了什么吗

case HttpRequest(HttpMethods.GET, Uri.Path("/ping"), _, _, _) =>
  HttpResponse(entity = "PONG")
应该是

case HttpRequest(HttpMethods.GET, Uri.Path("/ping"), _, _, _) =>
  sender ! HttpResponse(entity = "PONG")

您正在返回HttpResponse,而不是向发件人发送消息。

我不明白Scala编译器为什么没有抱怨HttpResponse是在预期的Unit类型时返回的。Ravi-Scala在预期的Unit类型时执行从任何值到Unit的隐式转换。-从规范:值丢弃。如果e有一些值类型,并且期望的类型是Unit,那么通过将e嵌入到术语{e;}中,将e转换为期望的类型。这不好。我想-从现在起,Ywarn value discard应该成为我所有SBT中的标准标志。
case HttpRequest(HttpMethods.GET, Uri.Path("/ping"), _, _, _) =>
  HttpResponse(entity = "PONG")
case HttpRequest(HttpMethods.GET, Uri.Path("/ping"), _, _, _) =>
  sender ! HttpResponse(entity = "PONG")