"complete" requires a ToResponseMarshallable, and "ToEntityMarshallable"
can be implicitly promoted to that, so you should be able to just return
"f" itself to "complete" and things should just work (if all the right
stuff is in scope):
complete {
val f: Future[Person] = ...
f
}
If you want more detailed control, for example if you don't want a 200 OK
response code, then you could provide a custom
ToResponseMarshaller[Person], or you could manually marshal the Person and
proceed from there:
complete {
val f: Future[Person] = ...
val ent: Future[HttpEntity] = f.flatMap(p => Marshal(p).to[HttpEntity])
HttpResponse(entity = ent)
}
Hope this helps,
Rich
On Monday, May 25, 2015 at 2:34:19 PM UTC+1, Jan Algermissen wrote:
>
> Hi,
>
> I have a question regarding custom marshallers and how to apply them in
> routes.
>
> My goal is to respond with JSON representations of person objects, using a
> custom media type 'application/vnd.foo.person+json’.
>
> I have a case class and implicit spray-json formats
>
> case class Person(firstname:String, surname:String)
>
> I also have written a media type and a marshaller:
>
> val m = MediaType.custom("application/vnd.foo.person+json",
> MediaType.Encoding.Fixed(HttpCharsets.`UTF-8`))
>
> implicit def PersonMarshaller[T](implicit writer: RootJsonWriter[T],
> printer: JsonPrinter = PrettyPrinter): ToEntityMarshaller[Person] =
> Marshaller.StringMarshaller.wrap(ContentType(m))(p =>
> printer.apply(p.toJson))
>
>
> And I have the following route where I just cannot figure out the correct
> directive to have akka-http apply the implicit marshaller
>
>
> def status(statusActor: ActorRef) = {
> path("status") {
> get {
> complete {
> val f:Future[Person] = …. obtain future person object from some
> foo.findPerson(…)
> f.map(p =>
> ? <———————— what to do here?
> )
> }
> }
> }
> }
>
> Can anyone enlighten me?
>
> I tried with
> f.map(p => HttpResponse(entity = HttpEntity(m, p)))
>
> but HttpEntity does not seem to be the place to hand the ‘domain object’
> to
>
> I also tried
>
> HttpResponse(entity = p)
>
> assuming the marshaller would be used to turn Person into Entity but that
> doesn’t compile either
>
> Jan
>
>
>
>
>
--
>>>>>>>>>> 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.