Can anyone help me because i really don't know how to resolve my issue:
- 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
}
]
I'd like to just send a
GETrequest by using Rest Assured with POJO ClassesMy 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;
}
}
- Second class - Ids with ids of books
package pojo;
public class Ids {
private String bookingid;
public String getBookingid() {
return bookingid;
}
}
- 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]
CodePudding user response:
I see many issues here:
You test class and outermost POJO are both called
Books. Which one are you trying to deserialise to?In the input JSON the key is named
bookingidand in the code it isbookingId(notice the uppercaseI.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 ofIdsand since this represents a single ID then it should probably namedId.
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);

