簡體   English   中英

更新來自 API 的 JSON 響應,並使用 Rest Assured 將更新后的響應作為輸入/正文傳遞給另一個 API

[英]Update JSON response from an API and pass the updated response as input/body to another API using Rest Assured

我目前正在使用 Rest-Assured 撥打 API,其響應正文發布在下方。 我需要選擇響應的一部分,即整個 WorkItems 標簽(第 3 行),並更新字段“CorrelationUId”和“Value”(WorkItemAttributes 標簽內的節點),其值需要設置為唯一值。

更新后的 JSON 將作為另一個 API 的主體。我如何使用 Rest-Assured 和 java 來實現這一點?

   {
    "TotalRecordCount": 1,
    "BatchSize": 500,
    "WorkItems": [{
        "CreatedByApp": "IssueManagement",
        "ItemState": 1,
        "StackRank": 0,
        "CorrelationUId": "05c0df91-cd6f-4f74-8e19-0be556879e59",
        "RowStatus": null,
        "WorkItemDeliveryConstructs": [{
                "CreatedByUser": "Gateway",
                "ItemState": 0
            }

        ],

        "WorkItemLanguages": null,
        "WorkItemProductInstances": [{
            "ModifiedOn": "2020-08-05T05:01:15.335316Z",
            "UserUId": null,
            "ItemState": 0
        }],
        "WorkItemAssociations": null,
        "WorkItemAttachments": null,
        "WorkItemAttributes": [{

                "IdValue": "00000000-0000-0000-0000-000000000000",
                "IdExternalValue": "",
                "Value": "enter unique data here",
                "ItemState": 0
            },

            {

                "IdValue": "00000000-0000-0000-0000-000000000000",
                "IdExternalValue": "",
                "Value": "",
                "ItemState": 0
            }
        ]


    }],
    "Faults": [],
    "StatusCode": 0,
    "MergeResult": null
}

下面是上面的代碼片段。

 RequestSpecification request = RestAssured.given();
request.header("Content-Type", "application/json")
 JSONObject requestParams = new JSONObject();
requestParams.put("data", Property.getProperty("data")); 
Response response = request.post(url);
JsonPath js = response.jsonPath();
JSONObject responseObject = new JSONObject(response.jsonPath().getJsonObject("WorkItems"));
 
 
Configuration configuration = Configuration.builder().jsonProvider(new JacksonJsonNodeJsonProvider()).mappingProvider(new JacksonMappingProvider()).build();
DocumentContext json = JsonPath.using(configuration).parse(jsonString);
String jsonPath = "WorkItems.CorrelationUId";
String newValue = "newCorrelationUId";
System.out.println(json.set(jsonPath, newValue).jsonString()); 

添加了以下依賴項,保證 Rest。 但是,我遇到了與 jayway 依賴項的導入沖突“導入 io.restassured.path.json.JsonPath 與另一個導入語句沖突”

<dependency>
    <groupId>com.jayway.jsonpath</groupId>
    <artifactId>json-path</artifactId>
    <version>2.4.0</version>
</dependency>

下面的代碼片段幫助我更新了節點值並使用 Jayway 選擇了整個 json 的一部分

  DocumentContext json = JsonPath.using(configuration).parse(file);
   String jsonPath  = "WorkItems[0].WorkItemAttributes[0].Value";
String newValue = "new title";
DocumentContext finaljson = json.set(jsonPath, newValue);
 DocumentContext context = JsonPath.parse(finaljson.jsonString());
HashMap<String, Object> requiredpartofJson= context.read("WorkItems[0]");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM