SAP ERP中实现RESTfull

创建类

实现接口:IF_HTTP_EXTENSION

读取Request中Header信息

"get request info
lv_path_info = server->request->get_header_field( name = '~path_info' ).
*POST/GET...
lv_request_method = server->request->get_header_field( name = '~request_method' ).

读取Body中的信息

lv_json = server->request->get_cdata( ) .

设置Response信息

*Header
CALL METHOD server->response->set_header_field( name = 'Allow' value = 'Post' ).
*Set Status
CALL METHOD server->response->set_status( code = '405' reason = 'Method not allowed' ).

返回json

DATA: lv_path_info      TYPE string,
      lv_request_method TYPE string,
      lv_response_data  TYPE string.
DATA: BEGIN OF ls_msg,
        msgty   TYPE c,
        message TYPE string,
        ecmurl  TYPE string,
        data    TYPE zrre_s_file_id,
      END OF ls_msg.

CALL METHOD server->response->set_header_field( name = 'Content-Type' value = 'application/json; charset=UTF-8' ).
"Set Response Data
lv_response_data =  /ui2/cl_json=>serialize( data             = ls_msg ).
CALL METHOD server->response->set_cdata( data = lv_response_data ).
CALL METHOD server->response->set_status( code = '200' reason = 'OK' ).
*CALL METHOD server->response->set_status( code = '400' reason = 'Error' ).

发布服务

进入SICF创建服务后双击进入服务配置,在处理器清单页签填写创建的类,保存,激活
1691734782060.png