【问题标题】:How to return large text in ORDS using JSON (more than 32k)如何使用 JSON 在 ORDS 中返回大文本(超过 32k)
【发布时间】:2019-03-26 17:12:57
【问题描述】:

我在 Oracle ORDS 中创建了一个 Rest API,我需要返回一个大于 32k 的 base64 文本。源类型是 PL/SQL。

原始数据在 CLOB 变量中,但 ORDS 不支持这种返回。我尝试使用 LONG,但是当字符串大于 32k 时,我无法将字符串移动到 LONG。

  • 我尝试将内容从 CLOB 移动到 LONG,但没有成功。
  • 我尝试使用我需要的文本创建两个 Long 变量,并将其连接到 Long 变量以输出它,但也没有成功。
  • 我能够在 ResultSet 中返回内容,但它会使 Json 结构与我需要的不同。
--This is the variable that has the large text (about 40k characters)
out_hexa        CLOB;

-- :boleto 是 ORDS 中的 OUT 参数 (OUT, RESPONSE, LONG)

--This wont work:
:boleto := out_hexa;
--This wont work:
:boleto := substr(out_hexa, 1, 32765) || substr(out_hexa, 32765, LENGTH(out_hexa));

--这可行,但 Json 输出不是我想要的方式,因为它在 Json 中创建了第二个级别 重要提示:在这种情况下,:boleto 是 ResultSet,而不是 Long

OPEN :boleto FOR
     SELECT out_hexa as dados from dual;
In this case the output is:
{
    "boleto": [
        {
            "dados": "JVBERi0xLjQKMSAwIG9iago8PAovVGl0bGUgKP7..."
        }
     ]
}


What I need is a Json in this format:
{
    "boleto": "JVBERi0xLjQKMSAwIG9iago8PAovVGl0bGUgKP7..."
}

【问题讨论】:

  • ords 支持返回的 LOB,将源类型设置为媒体资源,将 mime 类型设置为 application/base64 - 类似于 thatjeffsmith.com/archive/2017/09/ords-returning-raw-json
  • 您好,好的,但是在这种情况下,我需要返回一个“application/json”mime 类型,并且一个字段的内容将是 base64 字符串....有什么想法吗?谢谢!
  • 自己生成json
  • 是的,就是这样......我正在自己编写 Json 及其工作。谢谢!

标签: plsql oracle-ords


【解决方案1】:

找不到自动执行此操作的方法,所以我自己编写 Json。 我正在分块读取 CLOB 并使用 HTP.prn 编写它:

      OWA_UTIL.mime_header('application/json', FALSE);
      OWA_UTIL.http_header_close;         
      htp.prn('{');
      htp.prn('"return_code" : "' ||out_status || '",');
      htp.prn('"return_message" : "' ||out_msg_retorno || '",');
      htp.prn('"boleto" : "');

      IF(out_status = '001') THEN
        :http_status      := 200;                    
        WHILE counter < length(out_hexa)+chunk_size LOOP                     
            htp.prn(substr(out_hexa, counter, chunk_size));
            counter := counter+chunk_size;
        END LOOP;        
      ELSE
        :http_status      := 404;
      END IF;
      htp.prn('"}');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-05
    • 2014-06-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-22
    • 1970-01-01
    • 2019-01-09
    • 2017-08-12
    相关资源
    最近更新 更多