Akka-Http Marshalling and Unmarshalling in JAVA

Reading Time: 3 minutes
Akka HTTP offers a general toolkit for providing and consuming HTTP-based services.

Hello folks! In today’s HTTP request/response world, we may get different types of data from a client side application which we need to process and send an appropriate response. This may get a bit of a mess. To solve this, we can use the marshalling and unmarshalling feature of Akka-Http for easier conversion of theses data. In this blog, we will be looking how to use these features in our project.

Akka-Http is an Akka based http library for building RESTful services in java and scala. Akka HTTP’s (un)marshalling feature make it very easy for the developers to convert application domain objects to and from JSON. There is also (un)marshalling support for XML and binary encodings in Akka HTTP however this XML support is currently not available as a Java API. Integration for Jackson is provided through the akka-http-jackson module. 

How to Include it in your build?

Support module for (un)marshalling to and from JSON along with Jackson support can be used by adding the following dependencies:

Maven:

<dependency>
  <groupId>com.typesafe.akka</groupId>
  <artifactId>akka-http-jackson_2.12</artifactId>
  <version>10.1.9+3-8c13e4b3+20190823-1353</version>
</dependency>

Gradle:

dependencies {
  compile group: 'com.typesafe.akka', name: 'akka-http-jackson_2.12', version: '10.1.9+3-8c13e4b3+20190823-1353'
}

What is Marshalling?

Conversion of a higher level object into some kind of lower level object of called Marshalling. In other words, marshalling means converting an object of type T into a lower level object.

Marshalling Design:

Marshalling of an object of type A into object of type B is performed by Marshaller<A,B>. 

For an object of type A, Marhsaller<A,B> produces:

  1. CompletionStage: Marshallers return a future which allows asynchronous execution in the marshalling process.
  2. List: Marshallers can return a number of representation for object of type A. Content negotiaton is used for deciding which representation will has to be used.
  3. Marshalling<B>: Marshallers first produce a Marshalling<B> before producing instance of B. This enables content negotiation and allows for delaying the actual construction of marshalling target instance.

Other names for marshalling: Serialization, Pickling

What is Unmarshalling?

Unmarshalling is just the opposite of marshalling. It has to be used for converting a lower level object into a higher level object.

In Akka HTTP unmarshalling means conversion of low level object into a high level object of type T.

Unmarshalling Design:

Unmarshalling of an object of type A to high level object of type B is done by Unmarshaller<A,B>.

Unmarshaller<A,B> is quite similar to Function<A, CompletionStage<B>> in its structure and is also very simple as compared to Marshaller<A,B>. Unmarshalling does not support content negotiation. Thus, there is no requirement of a List of results. This results in two lesser layers of indirection which is required in the marshalling process.

Other names for unmarshalling: Deserialization, Unpickling

An Example of Marshalling/Unmarshlling

post(() ->
        path("akkaHttpPath", () -> {
            final ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.registerModule(new JavaTimeModule());
//Converting JSON file to an object.
            return entity(Jackson.unmarshaller(objectMapper, somePOJOClass.class), recievedObject -> {
                final ArrayList<HttpHeader> headers = getCORSHeaders();
                return respondWithHeaders(headers, () -> 
//Converting an Object to a JSON file.
CompleteOKWithFuture(someClass.someFunction(recievedObject),
Jackson.marshaller()));
           });
        })
      )

Conclusion

In conclusion, Akka-HTTP’s marshalling and unmarshalling provides a simple solution to easily convert higher level objects to lower level objects and the other way as well. Therefore, developers now don’t have to provide methods for the same. In other words we can increase our routes’ effectiveness with less line of codes!

References:

https://doc.akka.io/docs/akka-http/10.0.10/java/http/routing-dsl/marshalling.html
https://developer.lightbend.com/guides/akka-http-quickstart-scala/json.html

Leave a Reply

Knoldus Pune Careers - Hiring Freshers

Get a head start on your career at Knoldus. Join us!