RestAssured #13 - Converting response payload in JSON to POJO using De-Serialization

Using de-serialization to convert JSON to POJO

In this section, we are going to see how to convert a response in json to POJO using De-serialization. To get a response in json using GET request will use this endpoint https://reqres.in/api/users which has the below JSON -

First, we need to create a POJO class for the above JSON. Note that I have used annotation @JsonProperty to map instance member name with json key name for "per_page" and "total_pages"

public class UsersPOJO {
    private int page;
    @JsonProperty("per_page")
    private int perPage;
    private int total;
    @JsonProperty("total_pages")
    private int totalPages;
    private List<Data> data;
    private Support support;

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPerPage() {
        return perPage;
    }

    public void setPerPage(int perPage) {
        this.perPage = perPage;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public int getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(int totalPages) {
        this.totalPages = totalPages;
    }

    public List<Data> getData() {
        return data;
    }

    public void setData(List<Data> data) {
        this.data = data;
    }

    public Support getSupport() {
        return support;
    }

    public void setSupport(Support support) {
        this.support = support;
    }



    public static class Data{
        private int id;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;
        }

        public String getFirst_name() {
            return first_name;
        }

        public void setFirst_name(String first_name) {
            this.first_name = first_name;
        }

        public String getLast_name() {
            return last_name;
        }

        public void setLast_name(String last_name) {
            this.last_name = last_name;
        }

        public String getAvatar() {
            return avatar;
        }

        public void setAvatar(String avatar) {
            this.avatar = avatar;
        }

        private String email;
        private String first_name;
        private String last_name;
        private String avatar;
    }

    public static class Support{
        private String url;

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }

        private String text;
    }
}

Next will send a GET request to the endpoint and map the response to POJO

public class Users {
    private final String baseUri = "https://reqres.in/";
    private final String usersEndpointPath = "api/users";

    public Users(){
        RestAssured.baseURI = baseUri;
    }

    public UsersPOJO convertResponseBodyToPOJO(){
        Response response = RestAssured.given().get(usersEndpointPath);
        response.then().log().body();
        UsersPOJO usersPOJO = response.as(UsersPOJO.class);
        return usersPOJO;
    }
}

To test this I have added assertion on "total_pages" with value 2

public class UsersPOJOTest {
    Users users;
    @BeforeMethod
    public void setup(){
        users = new Users();
    }
    @Test
    public void convertResponseBodyToPOJOTest(){
        UsersPOJO usersPOJO = users.convertResponseBodyToPOJO();
        Assert.assertEquals(usersPOJO.getTotalPages(),2);
    }
}

Git Hub Repo Link - https://github.com/sksingh329/RestAssuredTutorials

Did you find this article valuable?

Support SUBODH SINGH by becoming a sponsor. Any amount is appreciated!