matinal:SAP ABAP 发布HTTP RestFul服务全过程解析(二)

2023-11-12 08:29:56 浏览数 (3)

1.SE24建立处理类

2.实现接口 IF_HTTP_EXTENSION

3.实现方法IF_HTTP_EXTENSION~HANDLE_REQUEST

代码语言:javascript复制
METHOD if_http_extension~handle_request.
   DATA : lr_json_ser TYPE REF TO cl_trex_json_serializer,
          lr_json_des TYPE REF TO cl_trex_json_deserializer.
   DATA : lv_json_string TYPE string.
   DATA : lv_request TYPE string.
   DATA : BEGIN OF lw_request,
            werks TYPE werks_d,
          END  OF lw_request.
   DATA lt_request LIKE TABLE OF lw_request.
   CLEAR lt_request.
   "获取传入JSON
   lv_json_string = server->request->get_cdata( ).
   "#JSON->ABAP内表
   CALL METHOD /ui2/cl_json=>deserialize
     EXPORTING
       json = lv_json_string
     CHANGING
       data = lt_request.
   "#返回JSON数据格式
   CALL METHOD server->response->if_http_entity~set_content_type
     EXPORTING
       content_type = 'applicaion/json'.
   "#获取数据
   SELECT
     matnr,
     werks
     INTO TABLE @DATA(lt_response)
     FROM marc
     UP TO 100 ROWS
     FOR ALL ENTRIES IN @lt_request
     WHERE werks = @lt_request-werks.
   CLEAR lv_json_string.
   "#返回内表数据ABAP转JSON
   CREATE OBJECT lr_json_ser
     EXPORTING
       data = lt_response.
   CALL METHOD lr_json_ser->serialize( ).
   CALL METHOD lr_json_ser->get_data
     RECEIVING
       rval = lv_json_string.
   "#返回JSON数据
   server->response->set_cdata(
   EXPORTING
   data = lv_json_string ).
 ENDMETHOD.

4.发布服务

4.1 SICF->创建服务

4.2 配置登陆数据(如在次配置配置则免密登录)

4.3 处理器清单输入实现类的名称

4.4 激活服务,POSTMAN测试

0 人点赞