Rest Assured : Serialisation and De-Serialisation
How to use Serialisation in Rest Assured

Rest Assured : Serialisation and De-Serialisation

In this article I have explained how to use serialisation and deserialisation to process request and response in Rest Assured. Below are sections covered-

  • What is serialisation and deserialisation? 
  • Why to use serialisation and deserialisation in REST API automation?
  • What is POJO in Java?
  • ‎Serialisation of request body into JSON 
  • De-Serialisation of JSON response into POJO 
What is serialisation and deserialisation? 
  • Serialisation is the process of translating a data structure or object state into a format that can be stored or transmitted and reconstructed later. The opposite operation, extracting a data structure from a series of bytes, is deserialisation. 
  • Why serialisation is used? Application involving multiple platforms, requires architecture independence. Serialising the data structure in an architecture-independent format means preventing the problems of byte ordering, memory layout, or simply different ways of representing data structures in different programming languages.
Why to use serialisation and deserialisation in REST API automation?

To access REST service we have to transfer data between client and REST service. Using serialisation, object having request details can be serialised into JSON/XML format which can be handled by REST service. And using deserialisation REST response which can be in JSON/XML format can be converted back to objects which be consumed by client. 

What is POJO in Java?
  1. What is POJO (Plain Old Java Object) class in Java?

‎POJO is a simple data structure that has private fields with public getter and setter, but it does not have business implementation. It is used to improve readability and reusability of a program. 

POJO should not 

  • ‎extend pre-specified class 
  • ‎implement pre-specified interface 

POJO should follow below property 

  • ‎class must be public 
  • ‎instance variables must be private 
  • ‎must have public default constructor 
  • ‎it should have getter/setter method for accessing/updating instance variables 
Serialisation of POJO into JSON 

‎1. Jackson library are used for serialisation and deserialisation. Below dependency should be added in project to work with Jackson in java. 

  • jackson-databind, jackson-annotations, jackson-core

2. In order to work with serialisation or deserialisation to send request or to process response, we have to create POJO class in Java.

‎3. Using POJO class in java and Jackson library we can serialise REST request i.e. we can convert POJO into byte of streams (JSON/XML) which will send to endpoint and get processed by service. In below example I have used Jira api’s to create issue using REST request. This request require json parameter which is having nested attribute in JSON. 

No alt text provided for this image

Consider below points in above JSON 

  1. ‎summary and description are String parameter 
  2. ‎project is a nested attribute which contains key parameter 
  3. ‎issue type is nested attribute which contains name parameter 
  4. ‎All of above parameter are present inside attribute field. 

‎To create POJO class for such request we need to have separate POJO classes created as below where we have separate class for each attribute section. Below is how we create POJO classes for above JSON and how it get processed. 

No alt text provided for this image


De-Serialisation of JSON response to POJO

‎1. For understanding de-serialisation, Let’s consider below JSON response for library API request. 

No alt text provided for this image

2. ‎For above JSON response, we can create below POJO class 

No alt text provided for this image

‎3. Below are how response are mapped to POJO class. 

No alt text provided for this image


Guidelines
  1. Getter methods are not needed for Request POJO class
  2. ‎Setter methods are not needed for Response POJO classes 
  3. ‎Name of instant variables in POJO classes are same s JSON attribute 

Great way of explanation.. Kudos

Thanks for sharing, such a clear explanation, I was indeed looking for why do we really need this.

To view or add a comment, sign in

More articles by Subodh Kumar Singh

  • Java #7 - Array (Part-2)

    Topics Multi-dimensional array (2-Dimensional Array) Array manipulations Multi-dimensional array (2-Dimensional Array)…

  • Java #6 - Array (Part-1)

    Topics What is an array? One-dimensional array Where is the array saved in memory? What is an array? A variable can…

  • Java #5 - Variables

    Topics Variable Types of variable Type Conversion and Casting Type inference - var Variable A variable is a name for a…

  • Java #4 - Data Type

    Topics Where does storage live? Where are primitive types and objects stored in memory? Primitive Types: Data Types in…

  • RestAssured#3-Why to automate webservices/REST API?

    Before diving into what and how to test REST API. In this article first lets see why we should bother to test/automate…

    4 Comments
  • RestAssured#2 - Introduction to REST API

    Topics What is REST API? REST API Principles HTTP Methods - GET, POST, PUT, PATCH and DELETE What is REST API? REST or…

  • RestAssured#1 - Introduction To API

    Topics What is API? Examples of API Usage How do API work? Types of API What is webservice and its type? Difference…

    2 Comments
  • Java #3 - Introduction to OOP

    In this article, will discuss on below topics:- What are programming paradigm? What is OOP? Features of OOP? Is Java a…

  • Java #2 - Java Overview

    In this article, will discuss on below topics:- How Java is platform independent and secure? Writing First Java Program…

  • Java #1 - History of Java

    In this article, will discuss on below topics:- A brief history of C and C++ History of Java Why Java was created? A…

Others also viewed

Explore content categories