2015-05-11 23:05:18 +02:00
.. _-handleWith-:
handleWith
==========
Signature
---------
2015-06-19 15:35:24 +02:00
.. includecode2 :: /../../akka-http/src/main/scala/akka/http/scaladsl/server/directives/MarshallingDirectives.scala
2015-05-11 23:05:18 +02:00
:snippet: handleWith
Description
-----------
2015-10-09 15:19:36 +02:00
Completes the request using the given function. The input to the function is produced with
the in-scope entity unmarshaller and the result value of the function is marshalled with
the in-scope marshaller. `` handleWith `` can be a convenient method combining `` entity `` with
`` complete `` .
2015-05-11 23:05:18 +02:00
2015-10-09 15:19:36 +02:00
The `` handleWith `` directive is used when you want to handle a route with a given function of
2015-05-11 23:05:18 +02:00
type A ⇒ B. `` handleWith `` will use both an in-scope unmarshaller to convert a request into
type A and an in-scope marshaller to convert type B into a response. This is helpful when your
core business logic resides in some other class or you want your business logic to be independent
2015-05-21 13:14:54 +02:00
of the REST interface written with akka-http. You can use `` handleWith `` to "hand off" processing
to a given function without requiring any akka-http-specific functionality.
2015-05-11 23:05:18 +02:00
`` handleWith `` is similar to `` produce `` . The main difference is `` handleWith `` automatically
calls `` complete `` when the function passed to `` handleWith `` returns. Using `` produce `` you
must explicity call the completion function passed from the `` produce `` function.
See :ref: `marshalling <http-marshalling-scala>` and :ref: `unmarshalling <http-unmarshalling-scala>` for guidance
2015-05-21 13:14:54 +02:00
on marshalling entities with akka-http.
2015-05-11 23:05:18 +02:00
Examples
--------
The following example uses an `` updatePerson `` function with a `` Person `` case class as an input and output. We plug this function into our route using `` handleWith `` .
2015-05-21 17:34:46 +02:00
.. includecode2 :: ../../../../code/docs/http/scaladsl/server/directives/MarshallingDirectivesExamplesSpec.scala
2015-05-11 23:05:18 +02:00
:snippet: person-case-class
2015-05-21 17:34:46 +02:00
.. includecode2 :: ../../../../code/docs/http/scaladsl/server/directives/MarshallingDirectivesExamplesSpec.scala
2015-05-11 23:05:18 +02:00
:snippet: example-handleWith-with-json
The PersonJsonSupport object handles both marshalling and unmarshalling of the Person case class.
2015-05-21 17:34:46 +02:00
.. includecode2 :: ../../../../code/docs/http/scaladsl/server/directives/MarshallingDirectivesExamplesSpec.scala
2015-05-11 23:05:18 +02:00
:snippet: person-json-support