首页 > 解决方案 > 如何解析和获取一些,验证来自使用 Java 的放心 api 中的响应的值

问题描述

我开始放心地学习,并设法做出响应并从服务器获取数据。但我无法解析和验证值。对于我的研究,我正在尝试获取所有数据并解析和验证。如果你解释我怎么做做,我会很高兴。这是我的代码:

@Test
    public void getRequest() {
        
    
            String  key ="key";
            String token= "token";
          
            String id ="idvalue";
            Response response = given().spec(new RequestSpecBuilder().setBaseUri("https://api.trello.com/1").setContentType(ContentType.JSON)
                    .build())
                    .log().all()
                    .when()
                    .queryParams("key", key,
                           "token",
                                 token)
                               .pathParam("id", id)
                   .get("/cards/{id}");
                   response.then().statusCode(200)
//.body("name", hasItems("value"));//I want to check all of variables actually.this line not working.

           }

邮递员的回复也在这里:

{
    "id": "607ff8e3dd2e81327e55c05d",
    "checkItemStates": [],
    "closed": false,
    "dateLastActivity": "2021-04-21T10:05:23.805Z",
    "desc": "",
    "descData": null,
    "dueReminder": null,
    "idBoard": "607eee0d9f781d012c9407c9",
    "idList": "607f1a4e92b92d8fa44e07b8",
    "idMembersVoted": [],
    "idShort": 14,
    "idAttachmentCover": null,
    "idLabels": [],
    "manualCoverAttachment": false,
    "name": "güncellemekarti2",
    "pos": 114688,
    "shortLink": "5NTDEAXD",
    "isTemplate": false,
    "cardRole": null,
    "dueComplete": false,
    "due": null,
    "email": null,
    "labels": [],
    "shortUrl": "https://trello.com/c/5NTDEAXD",
    "start": null,
    "url": "https://trello.com/c/5NTDEAXD/14-g%C3%BCncellemekarti2",
    "idMembers": [],
    "badges": {
        "attachmentsByType": {
            "trello": {
                "board": 0,
                "card": 0
            }
        },
        "location": false,
        "votes": 0,
        "viewingMemberVoted": false,
        "subscribed": false,
        "fogbugz": "",
        "checkItems": 0,
        "checkItemsChecked": 0,
        "checkItemsEarliestDue": null,
        "comments": 0,
        "attachments": 0,
        "description": false,
        "due": null,
        "dueComplete": false,
        "start": null
    },
    "subscribed": false,
    "idChecklists": [],
    "cover": {
        "idAttachment": null,
        "color": null,
        "idUploadedBackground": null,
        "size": "normal",
        "brightness": "dark",
        "idPlugin": null
    }
}

标签: javaapirest-assured

解决方案


hasItems() 更适合数组 - 在你的情况下,试试这个:

.body("name", is("güncellemekarti2"));

推荐阅读