1

This code results in a compile error of could not find implicit value for parameter marshaller: spray.httpx.marshalling.ToResponseMarshaller[List[akka.actor.ActorRef]].

I don't think the problem is the ActorRef, as changing this to .mapTo[List[String]] shows the same compile error

In general, it's somewhat confusing how spray does marshalling with all the implicits - is there a way to make this explicit e.g. ListProtocol.marshal(value)?

import akka.actor.Actor
import spray.http.HttpResponse
import spray.http.HttpRequest
import spray.http.Uri
import spray.http._
import spray.routing._
import HttpMethods._
import akka.actor.ActorRef
import akka.pattern.ask
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.util.Success
import scala.util.Failure
import spray.http.StatusCodes.InternalServerError
import spray.json.DefaultJsonProtocol
import spray.httpx.SprayJsonSupport._
import spray.httpx.marshalling._
import spray.http._

class HttpApi(val manager: ActorRef) extends HttpServiceActor {

  def receive = runRoute {
    path("nodes") {
      get {
        onComplete(manager.ask(NodeList())(3.seconds).mapTo[List[ActorRef]]) {
          case Success(value) => {
            // Compile error happens here
            complete(value)
          }
          case Failure(ex) => {
            complete(InternalServerError, s"An error occurred: ${ex.getMessage}")
          }
        }
      }
    }
  }
}
Hamy
  • 20,662
  • 15
  • 74
  • 102
  • Followup thought - `spray-json` includes a marshaller for a List[T], wonder why that's not working here either – Hamy Sep 20 '14 at 18:24
  • you need complete(value) + to have marshaller for ActorRef – Eugene Zhulenev Sep 20 '14 at 18:46
  • Thanks for the tip. I'm still having the same issue, just now it's the proper compile error instead of the one resulting from me not using `complete(value)` – Hamy Sep 20 '14 at 18:52
  • There is a default marshaller provided for List[T], so this should work for List[ActorRef], but I changed it to List[String] to be sure that was not the issue. – Hamy Sep 20 '14 at 18:53

1 Answers1

7

Change this import

import spray.json.DefaultJsonProtocol

to

import spray.json.DefaultJsonProtocol._

That is, you want to import the implicits defined in that object, not the object itself.

Alternatively you can extend the trait to pick up the implicits:

class HttpApi(val manager: ActorRef) extends HttpServiceActor
                                        with DefaultJsonProtocol {
AmigoNico
  • 6,652
  • 1
  • 35
  • 45