SAP 直接发布 SICF REST JSON 接口

2023-10-13 17:22:16 浏览数 (2)

介绍 此博客将指导您完成创建 SICF 服务 (REST) 的过程。SICF 是一个 SAP 事务,用于使用 Internet Communication Manager (ICM) 和 Internet Communication Framework (ICF) 来维护 HTTP 通信服务。这在多种情况下都很有用。例如:某个系统可能没有正确的网关安装或配置来在其中创建服务。

将采取以下步骤来创建和测试此类服务:

  1. 创建结构和表类型
  2. 创建ZIF_REST接口。
  3. 创建处理程序类
  4. 为 GET 请求创建类
  5. 为 POST 请求创建类
  6. 在事务 SICF 中创建节点
  7. 测试 GET 方法
  8. 测试 POST 方法
代码语言:javascript复制
METHOD if_http_extension~handle_request.

    DATA: lw_fields TYPE ihttpnvp,
          lt_fields TYPE tihttpnvp.

    DATA: lv_reqstr  TYPE string,
          lv_reqxstr TYPE xstring,
          lv_resstr  TYPE string,
          lv_errstr  TYPE string.

    DATA: lv_tflag  TYPE string,
          lv_trcode TYPE string.

    DATA: lv_str1 TYPE string.

    DATA: lv_mtype TYPE comsgty,
          lv_msg   TYPE bapi_msg.

    DATA: ev_resp_xstring TYPE xstring.

    DATA: lv_codepage TYPE string.

    DATA: lv_dest    TYPE rfcdest,
          lv_url     TYPE string,
          lv_ctype   TYPE string,
          lv_request TYPE string.

    DATA: lv_response TYPE string,
          lt_headers  TYPE tihttpnvp.


    DATA: lv_filename TYPE string.

    DATA: lv_reportidlist TYPE string.

    CALL METHOD server->request->get_header_fields
      CHANGING
        fields = lt_fields.

    CALL METHOD server->request->if_http_entity~get_data
      RECEIVING
        data = lv_reqxstr.

    CALL METHOD server->request->if_http_entity~get_cdata
      RECEIVING
        data = lv_reqstr.

    REPLACE ALL OCCURRENCES OF cl_abap_char_utilities=>cr_lf   IN lv_reqstr WITH ''.
    REPLACE ALL OCCURRENCES OF cl_abap_char_utilities=>newline IN lv_reqstr WITH ''.

    CLEAR: lw_fields.
    LOOP AT lt_fields INTO lw_fields WHERE name = '~request_method' AND value = 'POST'.
    ENDLOOP.
    IF lw_fields IS INITIAL.
      lv_response = '{"ERRORTEXT":"本接口仅支持 POST 调用"}'."<ERRORTEXT>本接口仅支持 POST 调用</ERRORTEXT>
    ELSE.

      CALL FUNCTION 'ZFM_YQT001'
        EXPORTING
          reqstr = lv_reqstr
        IMPORTING
          resstr = lv_response.

      IF lv_response IS INITIAL.
        lv_response = '{"pageNum":0,"pageSize":0,"total":0,"pages":0,"list":[{"corpId":0}]}'.
      ENDIF.
    ENDIF.

    CALL METHOD server->response->if_http_entity~set_content_type
      EXPORTING
        content_type = 'application/json; charset=utf-8'. "application/json; charset=utf-8   text/xml; charset=utf-8

    CALL METHOD server->response->if_http_entity~set_cdata
      EXPORTING
        data   = lv_response
        offset = 0
        length = -1.

  ENDMETHOD.

0 人点赞