Skip to content

Instantly share code, notes, and snippets.

@pbzdyl
Last active December 31, 2015 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pbzdyl/8006272 to your computer and use it in GitHub Desktop.
Save pbzdyl/8006272 to your computer and use it in GitHub Desktop.
Issue with spray.io and json marshallers
could not find implicit value for parameter marshaller: spray.httpx.marshalling.ToResponseMarshaller[scala.concurrent.Future[Animal]]
(zoo ? GetRandomAnimal).mapTo[Animal]
^
import spray.routing.HttpService
import spray.json.{RootJsonWriter, DefaultJsonProtocol}
import akka.actor.{Props, Actor, ActorSystem}
import akka.util.Timeout
import akka.pattern.ask
import scala.concurrent.duration._
import scala.util.Random
import reflect.ClassTag
import spray.httpx.SprayJsonSupport._
import Zoo._
sealed trait Animal
case class Dog(name: String, color: String) extends Animal
case class Cat(name: String, age: Int) extends Animal
class Zoo extends Actor {
val random = new Random
def receive: Actor.Receive = {
case GetRandomAnimal =>
if (random.nextBoolean()) Dog("puppy", "black")
else Cat("sylvester", 5)
}
}
object Zoo {
case object GetRandomAnimal
}
trait AnimalService extends HttpService {
object JsonImplicits extends DefaultJsonProtocol {
implicit val CatJsonWriter = jsonFormat2(Cat)
implicit val DogJsonWriter = jsonFormat2(Dog)
implicit val animalFormat = lift {
new RootJsonWriter[Animal] {
def write(c: Animal) = c match {
case cat: Cat => CatJsonWriter.write(cat)
case dog: Dog => DogJsonWriter.write(dog)
}
}
}
}
val system = ActorSystem()
val zoo = system.actorOf(Props(classOf[Zoo]))
case object GetRandomAnimal
implicit val timeout = Timeout(3.seconds)
import JsonImplicits._
val routes = path("/randomAnimal") {
post {
complete {
(zoo ? GetRandomAnimal).mapTo[Animal]
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment