Last active
December 31, 2015 15:19
-
-
Save pbzdyl/8006272 to your computer and use it in GitHub Desktop.
Issue with spray.io and json marshallers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
could not find implicit value for parameter marshaller: spray.httpx.marshalling.ToResponseMarshaller[scala.concurrent.Future[Animal]] | |
(zoo ? GetRandomAnimal).mapTo[Animal] | |
^ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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