how to use scala spray detach?

2019-03-06 03:29发布

问题:

i'm trying to use spray detach as following:

path("") {
  get {
    detach {
      respondWithMediaType(`text/html`) { // XML is marshalled to `text/xml` by default, so we simply override here
        complete {
          <html>
            <body>
              <h1>Say hello to <i>spray-routing</i> on <i>Jetty</i>!</h1>
            </body>
          </html>
        }

      }
    }
  }
}

but i get following compilation error:

type mismatch; found : spray.routing.RequestContext => Unit required: spray.routing.directives.DetachMagnet respondWithMediaType(text/html) { // XML is marshalled to text/xml by default, so we simply override here ^

I looked at: https://github.com/spray/spray/wiki/Detach and I still have no idea what i'm doing wrong...

回答1:

You are looking at a deprecated documentation. If you to want to read the current documentation you have to look at the spray.io website.

Regarding your problem, the current documentation specifies a different signature for the detach directive - see here

def detach()(implicit ec: ExecutionContext): Directive0
def detach()(implicit refFactory: ActorRefFactory): Directive0
def detach(ec: ExecutionContext): Directive0

As you are passing your ExecutionContext implicitly I guess, you have to write the parenthesis:

detach() {
  respondWithMediaType(`text/html`) { // XML is marshalled to `text/xml` by default, so we simply override here
    complete { ...


标签: scala akka spray