I'm trying to create a marshaller using Akka Http for a nested map that I
want to return as Json from my Http API together with an appropriate status
code. In the real code the map is coming from a service call that might
fail. I would like to determine the status code based on this service call
result. I have a simple route like this:
implicit val materializer: Materializer
> val map = Map("1" -> Map ("1.1" -> 1.1 ))
val routes = pathPrefix("") {
get {
complete {
service.call match {
case Success(_) => OK -> map
> case Failure(_) => ServiceUnavailable -> map
}
}
}
}
With Spray all I had to do was create a simple marshaller like this:
> implicit val mapMarshaller =
>
> Marshaller.of[Map[String, Any]](`application/json`) { (value,
>> contentType, ctx) =>
>
> ctx.marshalTo(HttpEntity(contentType, toJson(value))) // toJson is
>> just using jackson to transform any object structure to a string
>
> }
>
>
As long this was available in implicit scope it allowed routes like the
above one. With Akka Http I tried something similar:
implicit val mapMarshaller = Marshaller.opaque[Map[String, Any],
>> HttpResponse] { map =>
>
> HttpResponse(entity =
>
> HttpEntity(ContentType(`application/json`, `UTF-8`),
>> JsonUtil.toJson(map))
>
> )
>
> }
>
>
This works if I pass the map directly to the 'complete' block, but not if
I'm combining with status code, like in the route above. So:
complete(OK, Map()) // this works
complete(OK -> Map()) // this does NOT work
I don't want to do the service call outside the complete, because that
makes it execute on startup.
What am I doing wrong? How can I achieve my goal?
--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ:
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.