Java RestAssured API POJO Class 无法从数组值(标记“JsonToken.START_ARRAY”)中反序列化“POJO.Category”类型的值

Java RestAssured API POJO Class Cannot deserialize value of type `POJO.Category` from Array value (token `JsonToken.START_ARRAY`)

我为 serializing/deserializing 创建了几个 POJO classes。当我创建一个接收所有类别的请求时,我希望接收一个类别对象数组。但我收到以下错误:-

Exception in thread "main" com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type POJO.Categories from Array value (token JsonToken.START_ARRAY)

见下文 classes:-

类别Class:-

package POJO;

public class Category {
    String _id;
    String name;
    Integer __v;
    String color;
    String icon;
    String id;
    
    public String get_id() {
        return _id;
    }
    public void set_id(String _id) {
        this._id = _id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer get__v() {
        return __v;
    }
    public void set_v(Integer __v) {
        this.__v = __v;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public String getIcon() {
        return icon;
    }
    public void setIcon(String icon) {
        this.icon = icon;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }

}

类别Class:-

package POJO;

import java.util.ArrayList;
import java.util.List;

public class Categories {
    ArrayList<Category> categories;

    public ArrayList<Category> getCategories() {
        return categories;
    }

    public void setCategory(ArrayList<Category> categories) {
        this.categories = categories;
    }
}

查看下面我的测试class:-

    import io.restassured.RestAssured;
    import io.restassured.parsing.Parser;
    import io.restassured.path.json.JsonPath;
    import io.restassured.response.Response;
    
    import static io.restassured.RestAssured.*;
    import static org.hamcrest.Matchers.*;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.testng.Assert;
    
    import POJO.Categories;
    import POJO.Category;

public class c_GetCategoryUsingPOJOClass {
    
    public static void main(String[] args) {
        
        RestAssured.baseURI = "https://eshop-backend-101.herokuapp.com/api/v1/";
        String getCategoriesResource = "categories";
            
            
            // Get all Categories
            Categories categoriesRes = given().log().all()

                .header("Content-Type", "application/json")
                .expect().defaultParser(Parser.JSON)
                
                .when().get(getCategoriesResource).as(Categories.class);
                
                // print the category name
                System.out.println("Categories:- ");
                System.out.println(categoriesRes);
    }

}

更多信息 - 当使用邮递员连接到 GET 类别端点时,这是我收到的响应正文:-

[
    {
        "_id": "5f15d467f3a046427a1c26e1",
        "name": "Mobile",
        "__v": 0,
        "color": "#6e4035",
        "icon": "mobile",
        "id": "5f15d467f3a046427a1c26e1"
    },
    {
        "_id": "5f15d5b2cb4a6642bddc0fe7",
        "name": "House",
        "__v": 0,
        "color": "#E2E1F0",
        "icon": "home",
        "id": "5f15d5b2cb4a6642bddc0fe7"
    },
    {
        "_id": "5f9d7e938680aa1d979d7e19",
        "name": "Games",
        "__v": 0,
        "color": "#ffb8b8",
        "icon": "sun",
        "id": "5f9d7e938680aa1d979d7e19"
    },
    {
        "_id": "5f15d54cf3a046427a1c26e3",
        "name": "Computers",
        "__v": 0,
        "color": "#E1F0E7",
        "icon": "desktop",
        "id": "5f15d54cf3a046427a1c26e3"
    },
    {
        "_id": "5f15d545f3a046427a1c26e2",
        "name": "Beauty",
        "__v": 0,
        "color": "#F0E8DE",
        "icon": "palette",
        "id": "5f15d545f3a046427a1c26e2"
    },
    {
        "_id": "608fe08365074604f45ce544",
        "name": "Cameras",
        "icon": "camera",
        "color": "#ede4da",
        "__v": 0,
        "id": "608fe08365074604f45ce544"
    }
]

我现在通过测试class更新如下:-

import POJO.Category;
import io.restassured.RestAssured;
import io.restassured.common.mapper.TypeRef;
import io.restassured.parsing.Parser;

public class c_GetCategoryUsingPOJOClass {
    
    public static void main(String[] args) {
        
        RestAssured.baseURI = "https://eshop-backend-101.herokuapp.com/api/v1/";
        String getCategoriesResource = "categories";
            
            
            // Get all Categories
            List<Category> categoriesRes = given().log().all()

                .header("Content-Type", "application/json")
                .expect().defaultParser(Parser.JSON)
                
                .when().get(getCategoriesResource).as(new TypeRef<List<Category>>() {});
                
                // print the category name
                System.out.println("Categories:- ");
                System.out.println(categoriesRes);
                System.out.println(categoriesRes.get(0).toString());

    }

}

这里的问题出在你的 POJO 上,它将匹配 json 结构:

{
  "categories": [
    {...},
    {...}
  ]
}

但是你的json结构是

[
    {...},
    {...}
]

--> 你只需删除 Categories class 并更改反序列化 json 的方式,映射到 List<Category>

List<Category> as = given()...as(new TypeRef<List<Category>>() {});
System.out.println(as);

还有一件事,在打印对象之前覆盖 toString 方法。