Home > Blockchain >  Cannot deserialize instance of `Books` out of START_ARRAY token
Cannot deserialize instance of `Books` out of START_ARRAY token

Time:01-31

Can anyone help me because i really don't know how to resolve my issue:

  1. I have just simple GET request with such Array in response body:
  [
    {
        "bookingid": 12
    },
    {
        "bookingid": 8
    },
    {
        "bookingid": 25
    },
    {
        "bookingid": 6
    },
    {
        "bookingid": 29
    },
    {
        "bookingid": 20
    },
    {
        "bookingid": 17
    },
    {
        "bookingid": 22
    },
    {
        "bookingid": 30
    },
    {
        "bookingid": 26
    },
    {
        "bookingid": 3
    },
    {
        "bookingid": 2
    },
    {
        "bookingid": 18
    },
    {
        "bookingid": 14
    },
    {
        "bookingid": 9
    },
    {
        "bookingid": 11
    },
    {
        "bookingid": 4
    },
    {
        "bookingid": 13
    },
    {
        "bookingid": 15
    },
    {
        "bookingid": 19
    },
    {
        "bookingid": 7
    },
    {
        "bookingid": 5
    },
    {
        "bookingid": 21
    },
    {
        "bookingid": 24
    },
    {
        "bookingid": 27
    },
    {
        "bookingid": 33
    },
    {
        "bookingid": 31
    },
    {
        "bookingid": 1
    },
    {
        "bookingid": 23
    },
    {
        "bookingid": 28
    },
    {
        "bookingid": 32
    },
    {
        "bookingid": 16
    },
    {
        "bookingid": 10
    }
]
  1. I'd like to just send a GET request by using Rest Assured with POJO Classes

  2. My POJO Classes:

    3a. Books class with List object with Ids:

package pojo;
import java.util.List;

public class Books {
    private List<Ids> bookId;
    public List<Ids> getBookId() {
        return bookId;
    }
}
  1. Second class - Ids with ids of books
package pojo;

public class Ids {
    private String bookingid;

    public String getBookingid() {
        return bookingid;
    }
}
  1. Test class:
import io.restassured.RestAssured;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;

public class Books {

    @Test
    public void GetBooks() {
        RestAssured.baseURI = "https://restful-booker.herokuapp.com/booking";
        given().when().get().as(Books.class);
    }
}

When i'm running this test i have error:

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `Books` out of START_ARRAY token
 at [Source: (String)"[{"bookingid":10},{"bookingid":21},{"bookingid":4},{"bookingid":9},{"bookingid":14},{"bookingid":2},{"bookingid":19},{"bookingid":15},{"bookingid":5},{"bookingid":3},{"bookingid":12},{"bookingid":7},{"bookingid":20},{"bookingid":1},{"bookingid":16},{"bookingid":17},{"bookingid":13},{"bookingid":6},{"bookingid":11},{"bookingid":18}]"; line: 1, column: 1]

enter image description here

CodePudding user response:

I see many issues here:

  1. You test class and outermost POJO are both called Books. Which one are you trying to deserialise to?

  2. In the input JSON the key is named bookingid and in the code it is bookingId (notice the uppercase I.

  3. Why do you have an outer class Books (as POJO) at all? From the format of the JSON it seems more logical just to deserialise to array of Ids and since this represents a single ID then it should probably named Id.

CodePudding user response:

You have to deserialize to list of Book Ids.

This

 given().when().get().as(Books.class);

should be

given().when().get().as(Ids[].class);
  •  Tags:  
  • Related