I have created couple of POJO classes for serializing/deserializing. When I create a request to receive all categories I am expecting to receive an array of category objects. But I am getting the following error:-
Exception in thread "main" com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type POJO.Categories from Array value (token JsonToken.START_ARRAY)
1. This is my test class and I try to map the response to my POJO class(model) but it gives me an error.
@Test(alwaysRun = true, priority = 1)
public void getUserDetail() {
String url = "http://localhost:8081/users";
RequestSpecification httpRequest = RestAssured.given()
.header("Content-Type", "application/json")
.contentType(ContentType.JSON);
Response response = httpRequest.get(url);
ResponseBody responseBody = response.getBody();
UsersResponseDetails b = responseBody.as(UsersResponseDetails.class);
softAssert.assertEquals(b.getId(), 1,"invalid");
softAssert.assertAll();
}
2. Error
java.lang.RuntimeException: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `model.UsersResponseDetails` from Array value (token `JsonToken.START_ARRAY`)
at [Source: (String)"[{"id":1,"name":"litteleHelpByKR-1","dob":"2020-05-23T","addressDetails":{"houseNumber":"H-55","aadharNumber":32345,"addressLine1":"Mg Road-1","addressLine2":"12th Cross-1","landmark":"Near Airport Road","pin":1,"officeAddress":{"buildingNumber":3,"addressLine1":"XYZ-1","addressLine2":"Airport Road","landMark":"Near U turn signal","cardId":"EMP-001","pin":75943764}}}]"; line: 1, column: 1]
3. Used Jackson dependent but its not work
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.4</version>
</dependency>
4. API GET response
[
{
"id":1,
"name":"litteleHelpByKR-1",
"dob":"2020-05-23T",
"addressDetails":{
"houseNumber":"H-55",
"aadharNumber":32345,
"addressLine1":"Mg Road-1",
"addressLine2":"12th Cross-1",
"landmark":"Near Airport Road",
"pin":1,
"officeAddress":{
"buildingNumber":3,
"addressLine1":"XYZ-1",
"addressLine2":"Airport Road",
"landMark":"Near U turn signal",
"cardId":"EMP-001",
"pin":75943764
}
}
}
]
5. This model I created to map the response
@Getter
@Setter
public class UsersResponseDetails {
private int id;
private String name;
private String dob;
private AddressDetails addressDetails;
@Getter
@Setter
public static class AddressDetails {
private String houseNumber;
private int aadharNumber;
private String addressLine1;
private String addressLine2;
private String landmark;
private int pin;
private OfficeAddress officeAddress;
@Getter
@Setter
public static class OfficeAddress {
private int buildingNumber;
private String addressLine1;
private String addressLine2;
private String landMark;
private String cardId;
private int pin;
}
}
}
My suggesstion are:
UsersResponseDetails[] b = responseBody.as(UsersResponseDetails[].class);
softAssert.assertEquals(b[0].getId(), 1,"invalid");
List<UsersResponseDetails> b= responseBody.as(new TypeRef<List<UsersResponseDetails>>() {});
softAssert.assertEquals(b.get(0).getId(), 1,"invalid");