SAP发布REST/HTTP接口详解编程语言

1.SE24新建类:ZCL_REST_QUERY

SAP发布REST/HTTP接口详解编程语言

激活,然后添加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发布REST/HTTP接口详解编程语言

设置服务的用户名密码:

SAP发布REST/HTTP接口详解编程语言

添加处理类:

SAP发布REST/HTTP接口详解编程语言

保存激活.测试服务:

SAP发布REST/HTTP接口详解编程语言

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             = [email protected]' 
*    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处理类中添加断点,执行测试程序,测试结果如下:

SAP发布REST/HTTP接口详解编程语言

 数据已经传递过来了。

返回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 
      ).

返回结果:

SAP发布REST/HTTP接口详解编程语言

参考别人的:

  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.

补充1:token方式http处理

"拼接后续要使用的tken 
  CONCATENATE 'Bearer' re_login-access_token INTO re_login-access_token SEPARATED BY space. 
  lo_http_client->request->if_http_entity~set_content_type( content_type = 'application/json' ). 
  lo_http_client->request->if_http_entity~set_header_field( name = 'Accept' value = 'application/json' ). 
  lo_http_client->request->set_method( if_http_request=>co_request_method_post ). 
 
 
  lo_http_client->request->if_http_entity~set_header_field( name   = 'Authorization' value = re_login-access_token ).

token有放body的有放header的。。。再用的时候注意下就好了。不过这种取token再调业务接口的模式。。。有点反人类,反正不喜欢。

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/18723.html

(0)
上一篇 2021年7月19日 21:23
下一篇 2021年7月19日 21:26

相关推荐

发表回复

登录后才能评论