使用 Rest Assured response.jsonPath().getObject() returns 进行序列化 null
Serializing using Rest Assured response.jsonPath().getObject() returns null
我有一个 API 返回 JSON 看起来像这样
{"listings":[
{
"albumId": 1,
"id": 1,
"title": "accusamus beatae ad facilis cum similique qui sunt",
"url": "https://via.placeholder.com/600/92c952",
"thumbnailUrl": "https://via.placeholder.com/150/92c952"
},
{
"albumId": 1,
"id": 2,
"title": "reprehenderit est deserunt velit ipsam",
"url": "https://via.placeholder.com/600/771796",
"thumbnailUrl": "https://via.placeholder.com/150/771796"
},
{
"albumId": 1,
"id": 3,
"title": "officia porro iure quia iusto qui ipsa ut modi",
"url": "https://via.placeholder.com/600/24f355",
"thumbnailUrl": "https://via.placeholder.com/150/24f355"
},
{
"albumId": 1,
"id": 4,
"title": "culpa odio esse rerum omnis laboriosam voluptate repudiandae",
"url": "https://via.placeholder.com/600/d32776",
"thumbnailUrl": "https://via.placeholder.com/150/d32776"
},
{
"albumId": 1,
"id": 5,
"title": "natus nisi omnis corporis facere molestiae rerum in",
"url": "https://via.placeholder.com/600/f66b97",
"thumbnailUrl": "https://via.placeholder.com/150/f66b97"
},
]}
我想将 JSON 序列化到我的 MyObject
class。我只需要 2 个变量,所以我将忽略其他变量。
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown=true)
public class MyObject {
private String title;
private String url;
// Getters and Setters here
}
我的测试运行器看起来像这样
public class MyTest {
@Test
public void runTest() {
// Usual Rest-Assured GET request here
Response response = request
.request(Method.GET);
List < MyObject > objects = Arrays.asList(response.jsonPath().getObject("listings", MyObject[].class));
String testOneTitle = response.jsonPath().get("listings[0].title");
System.out.println("testOneTitle " + testOneTitle); // OK - Returns String of the title
System.out.println("Size " + objects.size()); // OK -Returns the size of the List
for (int z = 0; z < 3; z++) {
System.out.println("Title " + objects.get(z).getTitle()); // NOK - This returns null
}
}
}
Title null
Title null
Title null
我不知道为什么它 returns 无效。
我怀疑此方法没有正确序列化 JSON?
List < MyObject > objects = Arrays.asList(response.jsonPath().getObject("listings", MyObject[].class));
知道为什么吗?
从讨论中学习 ,我猜 Rest-Assured 使用第 3 方库来序列化 JSON ?不确定,但我通过添加依赖项
遵循关于 link 的建议
// https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.12.3'
我并没有真正详细地回答这个问题,因为我了解上下文,看来问题出在 POJO 上。这是我在本地机器上尝试过的东西,我能够得到预期的
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Listing {
@JsonProperty("listings")
public List<Listing> listings = null;
@JsonProperty("title")
public String title;
@JsonProperty("url")
public String url;
// Getters and Setters here
}
public static void main(String[] args) {
List<Listing> res = RestAssured.given().when().get("http://localhost:8080/stack").then().extract()
.response().as(Listing.class).getListings();
for (int z = 0; z < 5; z++) {
System.out.println("Title " + res.get(z).getTitle());
}
}
我有一个 API 返回 JSON 看起来像这样
{"listings":[
{
"albumId": 1,
"id": 1,
"title": "accusamus beatae ad facilis cum similique qui sunt",
"url": "https://via.placeholder.com/600/92c952",
"thumbnailUrl": "https://via.placeholder.com/150/92c952"
},
{
"albumId": 1,
"id": 2,
"title": "reprehenderit est deserunt velit ipsam",
"url": "https://via.placeholder.com/600/771796",
"thumbnailUrl": "https://via.placeholder.com/150/771796"
},
{
"albumId": 1,
"id": 3,
"title": "officia porro iure quia iusto qui ipsa ut modi",
"url": "https://via.placeholder.com/600/24f355",
"thumbnailUrl": "https://via.placeholder.com/150/24f355"
},
{
"albumId": 1,
"id": 4,
"title": "culpa odio esse rerum omnis laboriosam voluptate repudiandae",
"url": "https://via.placeholder.com/600/d32776",
"thumbnailUrl": "https://via.placeholder.com/150/d32776"
},
{
"albumId": 1,
"id": 5,
"title": "natus nisi omnis corporis facere molestiae rerum in",
"url": "https://via.placeholder.com/600/f66b97",
"thumbnailUrl": "https://via.placeholder.com/150/f66b97"
},
]}
我想将 JSON 序列化到我的 MyObject
class。我只需要 2 个变量,所以我将忽略其他变量。
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown=true)
public class MyObject {
private String title;
private String url;
// Getters and Setters here
}
我的测试运行器看起来像这样
public class MyTest {
@Test
public void runTest() {
// Usual Rest-Assured GET request here
Response response = request
.request(Method.GET);
List < MyObject > objects = Arrays.asList(response.jsonPath().getObject("listings", MyObject[].class));
String testOneTitle = response.jsonPath().get("listings[0].title");
System.out.println("testOneTitle " + testOneTitle); // OK - Returns String of the title
System.out.println("Size " + objects.size()); // OK -Returns the size of the List
for (int z = 0; z < 3; z++) {
System.out.println("Title " + objects.get(z).getTitle()); // NOK - This returns null
}
}
}
Title null
Title null
Title null
我不知道为什么它 returns 无效。
我怀疑此方法没有正确序列化 JSON?
List < MyObject > objects = Arrays.asList(response.jsonPath().getObject("listings", MyObject[].class));
知道为什么吗?
从讨论中学习
// https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.12.3'
我并没有真正详细地回答这个问题,因为我了解上下文,看来问题出在 POJO 上。这是我在本地机器上尝试过的东西,我能够得到预期的
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Listing {
@JsonProperty("listings")
public List<Listing> listings = null;
@JsonProperty("title")
public String title;
@JsonProperty("url")
public String url;
// Getters and Setters here
}
public static void main(String[] args) {
List<Listing> res = RestAssured.given().when().get("http://localhost:8080/stack").then().extract()
.response().as(Listing.class).getListings();
for (int z = 0; z < 5; z++) {
System.out.println("Title " + res.get(z).getTitle());
}
}