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);
Answer from spg on Stack OverflowI 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);
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)
If you need to get a value from response json list, here's what worked for me:
Json sample:
[
{
"first": "one",
"second": "two",
"third": "three"
}
]
Code:
String first =
given
.contentType(ContentType.JSON)
.when()
.get("url")
.then()
.extract().response().body().path("[0].first")
Actually, you can but... you need to handle deserialization issue of default mapper becase if you try do the following:
.extract().jsonPath().getList("findAll {it.productName == " + productName + "}", Card.class);
You will failing on converting HashMap to your object type. It happens because of using gpath expression in path provides json without double quotes on keys by default. So you need to prettify it with (you can put it in RestAssured defaults):
.extract().jsonPath().using((GsonObjectMapperFactory) (aClass, s) -> new GsonBuilder().setPrettyPrinting().create())
And as result your would be able to cast things like that:
.getObject("findAll {it.productName == 'productName'}.find {it.tariffPlanName.contains('tariffPlanName')}", Card.class)
See full example:
import com.google.gson.GsonBuilder;
import io.restassured.http.ContentType;
import io.restassured.mapper.factory.GsonObjectMapperFactory;
import lombok.Data;
import org.testng.annotations.Test;
import java.util.HashMap;
import java.util.List;
import static io.restassured.RestAssured.given;
public class TestLogging {
@Test
public void apiTest(){
List<Item> list = given()
.contentType(ContentType.JSON)
.when()
.get("https://jsonplaceholder.typicode.com/posts")
.then().log().all()
.extract().jsonPath().using((GsonObjectMapperFactory) (aClass, s) -> new GsonBuilder().setPrettyPrinting().create())
.getList("findAll {it.userId == 6}.findAll {it.title.contains('sit')}", Item.class);
list.forEach(System.out::println);
}
@Data
class Item {
private String userId;
private String id;
private String title;
private String body;
}
}
You can also do like this if you're only interested in extracting the "user_id":
String userId =
given().
contentType("application/json").
body(requestBody).
when().
post("/admin").
then().
statusCode(200).
extract().
path("user_id");
In its simplest form it looks like this:
String userId = get("/person").path("person.userId");
I found the answer :)
Use JsonPath or XmlPath (in case you have XML) to get data from the response body.
In my case:
JsonPath jsonPath = new JsonPath(responseBody);
int user_id = jsonPath.getInt("user_id");