I am glad to hear that. Sorry it took a month to get to this. :) On Fri, Aug 28, 2015 at 11:06 AM, Jeroen Rosenberg < [email protected]> wrote:
> Thanks Martynas, > > That solves it for me :) > > Jeroen > > On Thursday, August 27, 2015 at 2:44:33 PM UTC+2, Martynas Mickevičius > wrote: >> >> Hi Jeroen, >> >> to combine a marshaller for StatusCode and your custom marshaller, your >> marshaller has to be of type ToEntityMarshaller[T] as it is defined here >> <https://github.com/akka/akka/blob/bf16cfd118bb6e9904ead785db148f744c91ca7f/akka-http/src/main/scala/akka/http/scaladsl/marshalling/PredefinedToResponseMarshallers.scala#L28>. >> That is it has to marshal to HttpEntity and not to HttpResponse. >> >> So for your example to work, your marshaller should look like: >> >> implicit val mapMarshaller: ToEntityMarshaller[Map[String, Any]] = >> Marshaller.opaque { map => >> HttpEntity(ContentType(MediaTypes.`application/json`), map.toString) >> } >> >> >> >> On Mon, Jul 27, 2015 at 2:07 PM, Jeroen Rosenberg <[email protected]> >> wrote: >> >>> 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. >>> >> >> >> >> -- >> Martynas Mickevičius >> Typesafe <http://typesafe.com/> – Reactive >> <http://www.reactivemanifesto.org/> Apps on the JVM >> > -- > >>>>>>>>>> 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. > -- Martynas Mickevičius Typesafe <http://typesafe.com/> – Reactive <http://www.reactivemanifesto.org/> Apps on the JVM -- >>>>>>>>>> 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.
