I have an API returning JSON which looks like this
{"listings":[
{
"albumId": 1,
"id": 1,
"title": "accusamus beatae ad facilis cum similique qui sunt",
"url": "https://via.placeholder.com/600/92c952",
"thumbnailUrl": "https://via.placeholder.com/150/92c952"
},
{
"albumId": 1,
"id": 2,
"title": "reprehenderit est deserunt velit ipsam",
"url": "https://via.placeholder.com/600/771796",
"thumbnailUrl": "https://via.placeholder.com/150/771796"
},
{
"albumId": 1,
"id": 3,
"title": "officia porro iure quia iusto qui ipsa ut modi",
"url": "https://via.placeholder.com/600/24f355",
"thumbnailUrl": "https://via.placeholder.com/150/24f355"
},
{
"albumId": 1,
"id": 4,
"title": "culpa odio esse rerum omnis laboriosam voluptate repudiandae",
"url": "https://via.placeholder.com/600/d32776",
"thumbnailUrl": "https://via.placeholder.com/150/d32776"
},
{
"albumId": 1,
"id": 5,
"title": "natus nisi omnis corporis facere molestiae rerum in",
"url": "https://via.placeholder.com/600/f66b97",
"thumbnailUrl": "https://via.placeholder.com/150/f66b97"
},
]}
I want to serialize that JSON to my MyObject
class. I just need 2 variables, so I will just ignore the others.
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown=true)
public class MyObject {
private String title;
private String url;
// Getters and Setters here
}
My test runner looks like this
public class MyTest {
@Test
public void runTest() {
// Usual Rest-Assured GET request here
Response response = request
.request(Method.GET);
List < MyObject > objects = Arrays.asList(response.jsonPath().getObject("listings", MyObject[].class));
String testOneTitle = response.jsonPath().get("listings[0].title");
System.out.println("testOneTitle " + testOneTitle); // OK - Returns String of the title
System.out.println("Size " + objects.size()); // OK -Returns the size of the List
for (int z = 0; z < 3; z++) {
System.out.println("Title " + objects.get(z).getTitle()); // NOK - This returns null
}
}
}
Title null
Title null
Title null
I don't know why it returns null.
I suspect this method doesn't serialize the JSON properly?
List < MyObject > objects = Arrays.asList(response.jsonPath().getObject("listings", MyObject[].class));
Any idea why?
Learning from the discussion here , I am guessing Rest-Assured uses 3rd party library to serialize JSON ? Not sure, but I follow the suggestion on that link by adding dependency
// https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.12.3'
I didn't really go through the question in detail as I understood the context, It seems like the issue is with the POJO. Here's something I tried in my local machine and I am able to get the expected
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Listing {
@JsonProperty("listings")
public List<Listing> listings = null;
@JsonProperty("title")
public String title;
@JsonProperty("url")
public String url;
// Getters and Setters here
}
public static void main(String[] args) {
List<Listing> res = RestAssured.given().when().get("http://localhost:8080/stack").then().extract()
.response().as(Listing.class).getListings();
for (int z = 0; z < 5; z++) {
System.out.println("Title " + res.get(z).getTitle());
}
}