• SAP发布REST/HTTP接口


    1.SE24新建类:ZCL_REST_QUERY

    激活,然后添加interface:IF_HTTP_EXTENSION并激活。

    2.实现IF_HTTP_EXTENSION~HANDLE_REQUEST:

      METHOD IF_HTTP_EXTENSION~HANDLE_REQUEST.
        DATA: LT_FIELDS       TYPE TIHTTPNVP,
              LV_HEADER_QUERY TYPE STRING,
              LV_DATA         TYPE STRING,
              LV_HTML         TYPE STRING.
    
        FIELD-SYMBOLS: <FS_FIELD>       LIKE LINE OF LT_FIELDS.
    
    *" get HEADER fields
        SERVER->REQUEST->GET_HEADER_FIELDS(
            CHANGING
              FIELDS = LT_FIELDS    " Header fields
          ).
        LV_DATA = SERVER->REQUEST->IF_HTTP_ENTITY~GET_CDATA( ).
    
        " Read the fields table and look for name "~query_string" -- this will contain the URL query
        READ TABLE LT_FIELDS
          WITH KEY NAME = '~query_string'
          ASSIGNING <FS_FIELD>.
        IF SY-SUBRC EQ 0.
          CONCATENATE '{"message": "You have entered'
                      <FS_FIELD>-VALUE
                      'as query parameter."}'
                 INTO LV_HTML SEPARATED BY SPACE.
    
    *" Output to HTML
          SERVER->RESPONSE->SET_CDATA(
            EXPORTING
              DATA   = LV_HTML    " Character data
    *        offset = 0    " Offset into character data
    *        length = -1    " Length of character data
          ).
        ENDIF.
      ENDMETHOD.

    3.事务代码SICF定义REST服务:

    设置服务的用户名密码:

    添加处理类:

    保存激活.测试服务:

    SAP调用测试:

     创建程序:ZLYTEST_REST

    REPORT zlytest_rest.
    DATA: lo_http_client   TYPE REF TO if_http_client,
          lv_service       TYPE string,
          lv_request       TYPE string,
          lv_len           TYPE i,
          lv_result        TYPE string,
          lx_result        TYPE xstring,
          lo_ixml          TYPE REF TO if_ixml,
          lo_streamfactory TYPE REF TO if_ixml_stream_factory,
          lo_istream       TYPE REF TO if_ixml_istream,
          lo_document      TYPE REF TO if_ixml_document,
          conv             TYPE REF TO cl_abap_conv_in_ce,
          lo_parser        TYPE REF TO if_ixml_parser.
    lv_service = 'http://gjyxcrmdev.crm.hisense.com:8000/sap/zrest'.
    
    lv_request = '<Request><Id>2912744</Id><Notes>testaskldjasd123</Notes><FaultSource>consumer</FaultSource>' &&
    '<FaultBattery>no</FaultBattery>' &&
    '<FaultScreen>no</FaultScreen>' &&
    '<FaultFirmware>no</FaultFirmware>' &&
    '<FaultLogicboard>no</FaultLogicboard>' &&
    '<FaultKeyboard>no</FaultKeyboard>' &&
    '<FaultSpeaker>no</FaultSpeaker>' &&
    '<FaultHdd>no</FaultHdd>' &&
    '<FaultPowersupply>no</FaultPowersupply>' &&
    '<FaultOtherDescription>123</FaultOtherDescription>'
     && '</Request>'.
    lv_len = strlen( lv_request ).
    
    cl_http_client=>create_by_url(
      EXPORTING
        url                = lv_service
      IMPORTING
        client             = lo_http_client
      EXCEPTIONS
        argument_not_found = 1
        plugin_not_active  = 2
        internal_error     = 3
        OTHERS             = 4 ).
    
    *lo_http_client->authenticate(
    *    username             = 'service@hisenseinhome.com.au'
    *    password             = 'pass' ).
    
    CALL METHOD lo_http_client->request->set_cdata
      EXPORTING
        data   = lv_request
        offset = 0
        length = lv_len.
    
    lo_http_client->send(
      EXCEPTIONS
        http_communication_failure = 1
        http_invalid_state         = 2 ).
    
    lo_http_client->receive(
      EXCEPTIONS
        http_communication_failure = 1
        http_invalid_state         = 2
        http_processing_failed     = 3 ).
    
    CLEAR lv_result .
    lx_result = lo_http_client->response->get_data( ).
    CALL METHOD lo_http_client->close.
    CHECK 1 = 1.

    在REST处理类中添加断点,执行测试程序,测试结果如下:

     数据已经传递过来了。

    返回JSON数据格式例子:

        DATA: lo_json_ser TYPE REF TO cl_trex_json_serializer,
              lo_json_des TYPE REF TO cl_trex_json_deserializer.
        DATA: json_string TYPE string.
        DATA:gt_t005u TYPE TABLE OF t005u.
    "设置返回数据格式
    CALL METHOD server->response->if_http_entity~set_content_type
          EXPORTING
            content_type = 'application/json'.
    SELECT * INTO TABLE gt_t005u FROM t005u UP TO 5 ROWS WHERE spras = '1' AND land1 = 'CN'.
    
          CREATE OBJECT lo_json_ser
            EXPORTING
              data = gt_t005u[].
    
          CALL METHOD lo_json_ser->serialize.
          CALL METHOD lo_json_ser->get_data
            RECEIVING
              rval = json_string.
    server->response->set_cdata(
            EXPORTING
              data   = json_string    " Character data
          ).

    返回结果:

    参考别人的:

      METHOD IF_HTTP_EXTENSION~HANDLE_REQUEST.
        DATA: LO_JSON_SER TYPE REF TO CL_TREX_JSON_SERIALIZER,
              LO_JSON_DES TYPE REF TO CL_TREX_JSON_DESERIALIZER.
        DATA: JSON_STRING TYPE STRING.
        DATA:GT_T005U TYPE TABLE OF T005U.
        DATA:GW_T005U TYPE T005U.
        DATA:LV_METHOD TYPE STRING.
    
        LV_METHOD = SERVER->REQUEST->GET_HEADER_FIELD( NAME = '~request_method' ).
    
        CASE LV_METHOD.
          WHEN 'GET'.
            SELECT * INTO TABLE GT_T005U FROM T005U UP TO 5 ROWS WHERE SPRAS = '1' AND LAND1 = 'CN'.
    
            CREATE OBJECT LO_JSON_SER
              EXPORTING
                DATA = GT_T005U[].
    
            CALL METHOD LO_JSON_SER->SERIALIZE.
    
            CALL METHOD LO_JSON_SER->GET_DATA
              RECEIVING
                RVAL = JSON_STRING.
    
            CALL METHOD SERVER->RESPONSE->SET_CDATA(
              EXPORTING
                DATA = JSON_STRING ).
            SERVER->RESPONSE->SET_STATUS( CODE = 200 REASON = 'OK' ).
          WHEN 'POST' OR 'PUT'.
            JSON_STRING = SERVER->REQUEST->IF_HTTP_ENTITY~GET_CDATA( ).
            CREATE OBJECT LO_JSON_DES.
            CALL METHOD LO_JSON_DES->DESERIALIZE(
              EXPORTING
                JSON = JSON_STRING
              IMPORTING
                ABAP = GW_T005U ).
            SERVER->RESPONSE->SET_STATUS( CODE = 200 REASON = 'OK' ).
    
    *        MODIFY GT_T005U FROM GW_T005U.
    
          WHEN 'DELETE'.
          WHEN OTHERS.
        ENDCASE.
    
        CALL METHOD SERVER->RESPONSE->IF_HTTP_ENTITY~SET_CONTENT_TYPE
          EXPORTING
            CONTENT_TYPE = 'application/json'.
    
    
      ENDMETHOD.
  • 相关阅读:
    Linux -- touch
    Linux -- ls
    Linux -- 手动新建用户
    Linux -- id
    Linux -- chfn
    Linux -- finger
    Linux -- newgrp
    浅谈java中线程和操作系统线程
    java虚拟机入门(五)- 常见垃圾回收器及jvm实现
    java虚拟机入门(四)-垃圾回收的故事
  • 原文地址:https://www.cnblogs.com/sapSB/p/9968054.html
Copyright © 2020-2023  润新知