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:

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;
}

Leave a Reply