REST Assured - Generic List deserialization

spg picture spg · Mar 20, 2013 · Viewed 32.1k times · Source

Let's say I have a Java Person class:

class Person {
    String name;
    String email;
}

With REST Assured, you can deserialize this JSON object

{"name":"Bob", "email":"[email protected]"} 

to a Java Person instance using

Person bob = given().when().get("person/Bob/").as(Person.class);

How does one use REST Assured to deserialize this JSON array

[{"name":"Bob", "email":"[email protected]"}, 
 {"name":"Alice", "email":"[email protected]"}, 
 {"name":"Jay", "email":"[email protected]"}]

into a List<Person>? For example, this would be handy:

List<Person> persons = given().when().get("person/").as(...);

Answer

spg picture spg · Mar 22, 2013

I found a way to achieve what I wanted:

List<Person> persons = given().when().get("person/").as(Person[].class);

UPDATE: Using Rest-Assured 1.8.1, looks like cast to List is not supported anymore. You need to declare and object array like this:

Person[] persons = given().when().get("person/").as(Person[].class);