title: Spray Akka Json Unmarshalling
body: <p>I've a problem about unmarshalling objects to Json via using spray - akka.</p>
  
  <p>When i'd like to use actors that returns Future[List[Person]] , it doesn't work.</p>
  
  <p>If i use dao object directly, it works.</p>
  
  <p>Here are my codes:</p>
  
  <p><strong>PersonDao.scala</strong></p>
  
  <pre><code>import scala.concurrent.ExecutionContext.Implicits.global
  import scala.concurrent.Future
  
  case class Person(id: Int, name: String, surname: String)
  
  object PersonDao {
  
    def getAll: Future[List[Person]] = Future {
      List[Person](Person(1, "Bilal", "Alp"), Person(2, "Ahmet", "Alp"))
    }
  }
  </code></pre>
  
  <p><strong>EntityServiceActor.scala</strong></p>
  
  <pre><code>import akka.actor.Actor
  import com.bilalalp.akkakafka.model.PersonDao
  import com.bilalalp.akkakafka.service.ServiceOperation.FIND_ALL
  
  object ServiceOperation {
  
    case object FIND_ALL
  
  }
  
  class EntityServiceActor extends Actor {
  
    override def receive: Receive = {
  
      case FIND_ALL =&gt; PersonDao.getAll
    }
  }
  </code></pre>
  
  <p><strong>ServerSupervisor.scala</strong></p>
  
  <pre><code>import akka.actor.{Actor, ActorRefFactory}
  import com.bilalalp.akkakafka.webservice.TaskWebService
  import spray.routing.RejectionHandler.Default
  
  
  class ServerSupervisor extends Actor with PersonWebService {
  
    implicit val system = context.system
  
    override def receive: Receive = runRoute(entityServiceRoutes)
  
    override implicit def actorRefFactory: ActorRefFactory = context
  }
  </code></pre>
  
  <p><strong>WebServiceTrait.scala</strong></p>
  
  <pre><code>import akka.util.Timeout
  
  import spray.routing.HttpService
  
  import scala.concurrent.duration._
  import scala.language.postfixOps
  
  import org.json4s.NoTypeHints
  import org.json4s.native.Serialization._
  
  trait WebServiceTrait extends HttpService {
  
    implicit def executionContext = actorRefFactory.dispatcher
  
    implicit val json4sFormats = formats(NoTypeHints)
  
    implicit val timeout = Timeout(120 seconds)
  }
  </code></pre>
  
  <p><strong>PersonWebService.scala</strong></p>
  
  <pre><code>trait PersonWebService extends WebServiceTrait with Json4sSupport {
  
    val json3sFormats = DefaultFormats
  
    val entityServiceWorker = actorRefFactory.actorOf(Props[EntityServiceActor], "entityServiceActor")
  
    val entityServiceRoutes = {
      pathPrefix("person") {
        pathEndOrSingleSlash {
          get {
            ctx =&gt; ctx.complete((entityServiceWorker ? FIND_ALL).mapTo[Person])
          }
        }
      }
    }
  }
  </code></pre>
  
  <p><strong>Application.scala</strong></p>
  
  <pre><code>import akka.actor.{ActorRef, ActorSystem, Props}
  import akka.io.IO
  import com.bilalalp.akkakafka.server.ServerSupervisor
  import spray.can.Http
  
  
  object Application extends App {
  
    implicit val system = ActorSystem("actorSystem")
  
    val mainHandler: ActorRef = system.actorOf(Props[ServerSupervisor])
    IO(Http)! Http.Bind(mainHandler, interface = Configuration.appInterface, port = Configuration.appPort)
  
  }
  </code></pre>
  
  <p>When i run this code, It gives nothing and waits for a while.</p>
  
  <p>After waiting browser gives this message:</p>
  
  <blockquote>
    <p>The server was not able to produce a timely response to your request.</p>
  </blockquote>
  
  <p>And console output is</p>
  
  <blockquote>
    <p>[ERROR] [11/22/2015 21:15:24.109]
    [actorSystem-akka.actor.default-dispatcher-7]
    [akka.actor.ActorSystemImpl(actorSystem)] Error during processing of
    request HttpRequest(GET,<a href="http://localhost:3001/person/,List(Host" rel="noreferrer">http://localhost:3001/person/,List(Host</a>:
    localhost:3001, Connection: keep-alive, Cache-C ontrol: no-cache,
    Pragma: no-cache, User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64)
    AppleWebKit/537.36 (KHTML, like Gecko) Maxthon/4.4.6.1000
    Chrome/30.0.1599.101 Safari/537.36, DNT: 1, Accept-Encoding: gzip,
    deflate, Accept-Language: tr-TR),Empty,HTTP/1.1)
    akka.pattern.AskTimeoutException: Ask timed out on
    [Actor[akka://actorSystem/user/$a/entityServiceActor#-1810673919]]
    after [120000 ms]. Sender[null] sent message of type
    "com.bilalalp.akkakafka.service.ServiceOperation$FIND_ALL$".
            at akka.pattern.PromiseActorRef$$anonfun$1.apply$mcV$sp(AskSupport.scala:415)
            at akka.actor.Scheduler$$anon$7.run(Scheduler.scala:132)
            at scala.concurrent.Future$InternalCallbackExecutor$.unbatchedExecute(Future.scala:599)
            at scala.concurrent.BatchingExecutor$class.execute(BatchingExecutor.scala:109)
            at scala.concurrent.Future$InternalCallbackExecutor$.execute(Future.scala:597)</p>
  </blockquote>
  
  <p>If i change PersonWebService.scala to this :</p>
  
  <pre><code>trait PersonWebService extends WebServiceTrait with Json4sSupport {
  
    val json3sFormats = DefaultFormats
  
    val entityServiceWorker = actorRefFactory.actorOf(Props[EntityServiceActor], "entityServiceActor")
  
    val entityServiceRoutes = {
      pathPrefix("person") {
        pathEndOrSingleSlash {
          get (
  //                    ctx =&gt; ctx.complete((entityServiceWorker ? FIND_ALL).mapTo[Person])
            ctx =&gt; ctx.complete(PersonDao getAll)
          )
        }
      }
    }
  }
  </code></pre>
  
  <p>It works and output is :</p>
  
  <blockquote>
    <p>[{"id":1,"name":"Bilal","surname":"Alp"},{"id":2,"name":"Ahmet","surname":"Alp"}]</p>
  </blockquote>
  
  <p>I'd like to use actors in spray routes. I don't know whether it is a bad practice or not because i'm newbie in akka and spray.</p>
  
  <p>How can i solve this? Any ideas?</p>
  
  <p>Thank you.</p>