假设我有一个 Java Person
类:
class Person {
String name;
String email;
}
使用 REST Assured,您可以反序列化这个 JSON 对象
{"name":"Bob", "email":"bob@email.com"}
到 Java Person
实例使用
Person bob = given().when().get("person/Bob/").as(Person.class);
如何使用 REST Assured 来反序列化这个 JSON 数组
[{"name":"Bob", "email":"bob@email.com"},
{"name":"Alice", "email":"alice@email.com"},
{"name":"Jay", "email":"jay@email.com"}]
变成List<Person>
?例如,这会很方便:
List<Person> persons = given().when().get("person/").as(...);
最佳答案
我找到了实现我想要的方法:
List<Person> persons = given().when().get("person/").as(Person[].class);
更新:使用 Rest-Assured 1.8.1,似乎不再支持强制转换为 List。您需要像这样声明和对象数组:
Person[] persons = given().when().get("person/").as(Person[].class);
关于java - REST Assured - 通用列表反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15531767/