REST Assured: De-Serialization of the API Response into a POJO

Profile picture for user arilio666

So Deserialization is the process of fetching the stream of data we send to the rest API service site and converting them back into the java object (POJO). So let us use the same serialization example of the goapi rest API site to deserialize our sent JSON payload.

public class serialDeserialTest {
    
@Test
public void DeserTest() throws {

    RestAssured.baseURI = "https://gorest.co.in/public/v1/users/"+id;

     
     firstPojo fp = RestAssured.given().when().get().as(firstPojo.class);
    System.out.println(fp.toString());
}
}
  • So to deserialize we have passed the rest API URL with the data-id endpoint we want.
  • Then we instantiated the firstPojo class where the List<dataPojo> is declared.
  • Because through there the object fp gets called and it will access the toString method of dataPojo class and get the list of payload into POJO object.
  • So using rest assured we called the as(firstPojo.class).

Let's see the output:

firstPojo [meta=null, data=[dataPojo [id=1408, name=trinity, email=dcs@matrix.com, gender=female, status=active]]]
PASSED: DeserTest

===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

Verdict:
So in this way, we can serialize and deserialize the data back and forth and use these to our advantage.