如何使用 Oracle PLSQL 评估以 HATEOAS 格式返回的一些 URL

问题描述 投票:0回答:1

例如:我从 GET 服务返回了如下响应。我想评估 URL: URL2.com和URL7.com进一步在同一进程中并存储结果。做到这一点的好方法是什么。我知道使用 UTL_HTTP 进行 GET 调用的方法,但在这种情况下,我应该对“父”URL 进行 GET 调用,然后对所有所需的子请求进行进一步 GET 调用吗?

{
    "items": [
        {
            "PersonId": 1,
            "PersonNumber": "2",
            "CorrespondenceLanguage": null,
            "BloodType": null,
            "DateOfBirth": "1970-03-11",
            "DateOfDeath": null,
            "CountryOfBirth": null,
            "RegionOfBirth": null,
            "TownOfBirth": null,
            "ApplicantNumber": null,
            "CreatedBy": "",
            "CreationDate": "2018-06-21T03:46:31.499+00:00",
            "LastUpdatedBy": "",
            "LastUpdateDate": "2021-05-25T10:52:05.751+00:00",
            "links": [
                {
                    "rel": "self",
                    "href": "URL1.com",
                    "name": "workers",
                    "kind": "item",
                    "properties": {
                        "changeIndicator": "X"
                    }
                },
                {
                    "rel": "canonical",
                    "href": "URL2.com",
                    "name": "workers",
                    "kind": "item"
                },
                {
                    "rel": "child",
                    "href": "URL3.com",
                    "name": "addresses",
                    "kind": "collection"
                }
            ]
        },
        {
            "PersonId": 2,
            "PersonNumber": "1",
            "CorrespondenceLanguage": null,
            "BloodType": null,
            "DateOfBirth": "1969-07-04",
            "DateOfDeath": null,
            "CountryOfBirth": null,
            "RegionOfBirth": null,
            "TownOfBirth": null,
            "ApplicantNumber": null,
            "CreatedBy": "X.X",
            "CreationDate": "2020-03-06T14:05:09+00:00",
            "LastUpdatedBy": "",
            "LastUpdateDate": "2021-04-26T23:01:31.867+00:00",
            "links": [
                {
                    "rel": "self",
                    "href": "URL5.com",
                    "name": "workers",
                    "kind": "item",
                    "properties": {
                        "changeIndicator": "X"
                    }
                },
                {
                    "rel": "canonical",
                    "href": "URL6.com",
                    "name": "workers",
                    "kind": "item"
                },
                {
                    "rel": "child",
                    "href": "URL7.com",
                    "name": "addresses",
                    "kind": "collection"
                }
            ]
        }
    ],
    "count": 25,
    "hasMore": true,
    "limit": 25,
    "offset": 0,
    "links": [
        {
            "rel": "self",
            "href": "URL8",
            "name": "workers",
            "kind": "collection"
        }
    ]
}
json oracle plsql hateoas
1个回答
0
投票

要使用 Oracle PL/SQL 评估以 HATEOAS 格式返回的 URL,您可以按照以下步骤操作:

  1. 解析 JSON 响应。
  2. 遍历 URL
  3. 发出 GET 请求
  4. 处理回复

这里有一个示例 PL/SQL 脚本可帮助您入门:

DECLARE
    l_response CLOB;
    l_json_obj APEX_JSON.T_VALUES;
    l_url VARCHAR2(4000); -- Adjust the length as needed
BEGIN
    -- Assuming l_response contains your JSON response
    -- Parse the JSON response
    APEX_JSON.PARSE(l_response);
    
    -- Iterate through the "items" array
    FOR i IN 1 .. APEX_JSON.get_count(p_path => 'items') LOOP
        -- Get the "links" array for each item
        APEX_JSON.get_values(p_values => l_json_obj, p_path => 'items[%d].links', p0 => i);
        
        -- Iterate through the "links" array
        FOR j IN 1 .. APEX_JSON.get_count(p_values => l_json_obj) LOOP
            -- Get the URL
            APEX_JSON.get_varchar2(p_values => l_json_obj, p_path => 'href', p0 => j, p_out => l_url);
            
            -- Check if the URL is one you want to evaluate
            IF l_url = 'URL2.com' OR l_url = 'URL7.com' THEN
                -- Make a GET request to the URL using UTL_HTTP
                -- Process the response and store the result as needed
                -- You can use UTL_HTTP.REQUEST and UTL_HTTP.RESPONSE functions here
            END IF;
        END LOOP;
    END LOOP;
    
    -- Clean up after parsing
    APEX_JSON.FREE_PARSE;
END;
/

此脚本将帮助您解析 JSON 响应并从“links”数组中提取 URL,并向您感兴趣的 URL 发出 GET 请求。 您需要根据您的具体要求添加错误处理和进一步处理。

© www.soinside.com 2019 - 2024. All rights reserved.