0

this is fragment of my router in spray based service:

path(baseUrl / version / "apps" / Segment / "users" / Segment) { (app, user) =>
  respondWithMediaType(MediaTypes.`application/json`) { ctx =>
    createProxy(ctx, management, GetUser(appId = UUID.fromString(app), userId = UUID.fromString(user)))
  }
}

...

def createProxy(ctx: RequestContext, service: ActorRef, message: AnyRef): Unit = {
  val ref = actorRefFactory.actorOf(ResponseProxy.props(ctx))
  service.tell(message, ref)
}

...

object ResponseProxy {
  def props(ctx: RequestContext): Props = Props(new ResponseProxy(ctx))
}

class ResponseProxy(ctx: RequestContext) extends Actor {

  import concurrent.duration._

  context setReceiveTimeout 30.second

  def receive = {
    case ReceiveTimeout =>
      ctx.complete(mapping.mapper.get.writeValueAsString(ResponseMessage("timeout")))
      context stop self

    case x: HttpResponse => ctx.complete(x)

    case x: AnyRef =>
      ctx.complete(mapping.mapper.get.writeValueAsString(x))
      context stop self
  }
}

i try to migrate on akka http, this is part of route:

get {
    pathPrefix(baseUrl / version / "apps" / JavaUUID / "users" / JavaUUID ) { (app : UUID , user : UUID) =>
      pathEndOrSingleSlash { ctx =>
        createProxy(ctx, management, GetUser(appId = app, userId = user))
    }
}

how i can return JSON?

there is no solution for actor based request processing? only on Future?

HoTicE
  • 573
  • 2
  • 13
  • Where are you trying to return a JSON? Is it with this line: `mapping.mapper.get.writeValueAsString`? – Yuval Itzchakov Dec 19 '16 at 12:43
  • ... `respondWithMediaType(MediaTypes.`application/json`) { ctx =>` here i am not use json parser from spray for some reasons. spray based application works fine – HoTicE Dec 19 '16 at 12:55
  • Did you read [this](http://doc.akka.io/docs/akka/2.4.4/scala/http/common/json-support.html)? – Yuval Itzchakov Dec 19 '16 at 13:03
  • thanks - this will solve 1st problem but i still need actor based request complete, without futures, ask and e.t.c – HoTicE Dec 20 '16 at 14:00
  • i found [this](https://markatta.com/codemonkey/blog/2016/08/03/actor-per-request-with-akka-http/). This works well :) – HoTicE Jan 10 '17 at 15:06

0 Answers0