REST Assured - Generic List deserialization
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);
for those who found out that accepted answer does not work anymore.
List<Entity> list = new ArrayList<>(); list = given() .contentType(CONTENT_TYPE) .when() .get(getRestOperationPath()) .then() .extract().body().as(list.getClass());
hopefully, you understand that getRestOperationPath is returning rest operation path; and CONTENT_TYPE is placeholder for your content type (application/json for example)
upd: checked different versions, behavior differs depending on version, so you might want to try different approaches
upd2: cleaner solution was pointed by @Arigion in comments:
to use .extract().body().jsonPath().getList(".", Entity.class);
To extract a Java List, and not an Array, from a JSON API response, you just have to remember to use jsonPath
rather than as
:
List<Person> persons = given() .when() .get("/person") .then() .extract() .body() // here's the magic .jsonPath().getList(".", Person.class);
Your json path can point to anywhere you expect to have a list of json objects in your body. in this example (and working for your question) it just points to the json root.
sidenode: rest-assured is internally using jackson for deserialization (for .jsonPath
as well as .as
)