

Akka-Http Marshalling and Unmarshalling in JAVA
source link: https://blog.knoldus.com/akka-http-marshalling-and-unmarshalling-in-java/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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:
- CompletionStage: Marshallers return a future which allows asynchronous execution in the marshalling process.
- 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.
- 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
Recommend
-
38
In May 2018 Microsoft patched an interesting vulnerability ( CVE-2018-0824 ) which was reported by Nicolas Joly of Microsoft'...
-
22
This is a story about encoding/json in the Go standard library. I’m not going to say this is bad Go. That would be heresy. But there is an aspect of marshalling that could be improved. Because it is in the standard library...
-
5
In this article, we’re going to dive into object marshalling. We’ll explain what it is, look at the Marshall module, and then go through an example. We’ll then go a step deeper and compare the _dump and self._load me...
-
4
Marshalling/Unmarshalling in Akka HTTP Reading Time: 4 minutes In this blog we are going to discuss about Marshalling/Unma...
-
15
Providing Security Using "Akka-Http" Reading Time: 3 minutesWhile building any application (small or big) Security is one of the most concerned issue, that should be handled properly by the deve...
-
8
Reading Time: 2 minutes Akka HTTP puts a lot of focus on testability of code. It has a dedicated module akka-http-testkit for testing rest api’s. When you use this testkit you are not need to run external web server or application server to...
-
8
Introduction to Akka-Http Reading Time: < 1 minuteKnoldus organized a half an hour session on 29 July 2016 at 4.30 PM. It covers a brief introduction to Akka-Http, why we should use Akka-Http over spay...
-
12
Expose Prometheus metrics for an Akka HTTP application Reading Time: 2 minutes Application monitoring is an essential thing to keep the application available every time. We can do application monitoring through...
-
1
在之前的文章中我们讲过了,jboss marshalling是一种非常优秀的java对象序列化的方式,它可以兼容JDK自带的序列化,同时也提供了性能和使用上的优化。 那么这么优秀的序列化工具可不可以用在netty中作为消息传递的方式呢? 答案当然是肯定的,在n...
-
12
netty系列之:使用Jboss Marshalling来序列化java对象 在JAVA程序中经常会用到序列化的场景,除了JDK自身提供的Serializable之外,还有一些第三方的产品可以实现对JAVA对象的序列化。其中比较有名的就是Google protobuf。当然,也...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK