Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

REST Assured deserialize JSON Array – Unrecognized field

I want to deserialize the response from: https://bookstore.demoqa.com/BookStore/v1/Books.

I have the Book class:

package test010;

public class Book {

    String isbn;
    String title;
    String subTitle;
    String author;
    String publish_date;
    String publisher;
    int pages;
    String description;
    String website;
}

and the test class:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

package test010;

import io.restassured.RestAssured;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.testng.annotations.Test;

import java.util.List;

public class test010 {
    @Test
    public void jsonArray(){


        RestAssured.baseURI = "https://bookstore.demoqa.com/BookStore/v1/Books";
        RequestSpecification httpRequest = RestAssured.given();
        Response response = httpRequest.get();

        JsonPath json = response.jsonPath();

        System.out.println(json.getList("books"));

        List<Book> allBooks = json.getList("books", Book.class);

        for (Book book: allBooks){
            System.out.println("Book title: " + book.title + " and author: " + book.author);
        }

    }

}

I’m getting the error:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "isbn" (class test010.Book), not marked as ignorable (0 known properties: ]) at [Source: (String)"{"isbn":"9781449325862","title":"Git Pocket Guide","subTitle":"A Working Introduction","aut ..

If I use:
@JsonIgnoreProperties(ignoreUnknown = true) I get only nulls, but I want the data.

>Solution :

One easy way to solve this problem is make all fields of Book class is public

public class Book {
    public String isbn;
    public String title;
    public String subTitle;
    public String author;
    public String publish_date;
    public String publisher;
    public int pages;
    public String description;
    public String website;
}

Other way is use @Data from Lombok library

import lombok.Data;

@Data
public class Book {
    String isbn;
    String title;
    String subTitle;
    String author;
    String publish_date;
    String publisher;
    int pages;
    String description;
    String website;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading