Rest Assured - deserialize Response JSON as List<POJO>

Wojtek picture Wojtek · Feb 12, 2014 · Viewed 37.8k times · Source

I have a POJO Artwork. I'm retrieving a List of those objects from a RESTful webservice in the HTTP response body in JSON format. I'm trying to write a Rest Assured-based test that would analyze the returned list. The code looks like this:

Response response = get("/artwork");
List returnedArtworks = response.getBody().as(List.class)

The problem is, I can't get Rest Assured to parse the returned JSON as List<Artwork>. Instead, I get a List<LinkedHashMap>. The map has a proper structure, i.e. could be mapped by Jackson to Artwork object, but I'd like to avoid mapping it manually.

JSON mappings in my model are OK, because when I map single object like this:

Artwork returnedArtwork = response.getBody().as(Artwork.class);

it works fine.

Is it possible to get returnedArtworks as List<Artwork>?

Answer

volatilevar picture volatilevar · Aug 3, 2014

You can do this:

List<Artwork> returnedArtworks = Arrays.asList(response.getBody().as(Artwork[].class));

The trick is to deserialize JSON to an array of objects (because there is no difference between the JSON string of an array or a list), then convert the array to a list.