喷涂路由不起作用

时间:2014-12-13 23:27:18

标签: scala routing spray

任何人都可以解释为什么这个例子只对:localhost:8080做出反应?

package com.example

import akka.actor.Actor
import spray.routing._
import spray.http._
import MediaTypes._

// we don't implement our route structure directly in the service actor because
// we want to be able to test it independently, without having to spin up an actor
class MyServiceActor extends Actor with MyService {

  // the HttpService trait defines only one abstract member, which
  // connects the services environment to the enclosing actor or test
  def actorRefFactory = context

  // this actor only runs our route, but you could add
  // other things here, like request stream processing
  // or timeout handling
  def receive = runRoute(myRoute)
}


// this trait defines our service behavior independently from the service actor
trait MyService extends HttpService {

  val myRoute =
    path("test1") {
      get {
        respondWithMediaType(`text/html`) { // XML is marshalled to `text/xml` by default, so we simply override here
          complete {
            <html>
              <body>
                <h1>test1</h1>
              </body>
            </html>
          }
        }
      }
    } ~
    path("test2") {
      get {
        respondWithMediaType(`text/html`) { // XML is marshalled to `text/xml` by default, so we simply override here
          complete {
            <html>
              <body>
                <h1>TEST SPRAY</h1>
              </body>
            </html>
          }
        }
      }
    }
}

1 个答案:

答案 0 :(得分:2)

我不知道你是如何启动你的服务器的,但是这段代码完全给出了你对localhost的预期test1:8080 / test1和test2 for test2

object MyServiceServer extends App {

  implicit val system = ActorSystem("my-service")

  val service = system.actorOf(Props[MyServiceActor], "my-service")


  IO(Http) ! Http.Bind(service, "localhost", 8080)
}