Einfacher REST-Service (ohne OData)

1. SE11: neue Tabelle ZREST_TAB:
MANDT:MANDT
ID:VBELN
CUSTOMER_NAME:NAME1
MOBILE:TELF1
EMAIL:AD_SMTPADR
DESCR:CHAR100
STATUS:CHAR10
ACTION_TAKEN:CHAR100
CLOSING_DATE:SYDATUM
CREATEDBY:SYUNAME
CREATEDON:SYDATUM
TIME:SYUZEIT

2. SNRO ZRESTC:NUMC10
3. Neue Klasse ZCL_WEBCO_RHANDLER und eine Überschreibung:

class ZCL_WEBCO_RHANDLER definition
  public
  inheriting from CL_REST_HTTP_HANDLER
  final
  create public .
public section.
  methods IF_REST_APPLICATION~GET_ROOT_HANDLER
    redefinition .
endclass.

if_rest_application~get_root_handler redefinition:

data lo_router type ref to cl_rest_router.
  create object lo_router.
 
  lo_router->attach(
    exporting
      iv_template      =  '/webco'    " Unified Name for Resources (-URI)
      iv_handler_class =  'ZCL_WEBCO_RESOURCEP'   " Object Type Name
  ).
  ro_root_handler = lo_router.

4. Neue Klasse ZCL_WEBCO_RESOURCEP und zwei Überschreibungen:

class ZCL_WEBCO_RESOURCEP definition
  public
  inheriting from CL_REST_RESOURCE
  final
  create public .
public section.
  methods IF_REST_RESOURCE~GET
    redefinition .
  methods IF_REST_RESOURCE~POST
    redefinition .
endclass.

if_rest_resource~get redefinition:

data: lv_string1 type vbeln,
      lv_string2 type string,
      ls_co      type zrest_tab,
      ui2_cl_json type ref to cl_trex_json_serializer.
 
  lv_string1 = mo_request->get_uri_query_parameter( iv_name = 'ID' ).
 
  call function 'CONVERSION_EXIT_ALPHA_INPUT'
    exporting
      input  = lv_string1
    importing
      output = lv_string1.
 
  select single * from zrest_tab into corresponding fields of ls_co where id = lv_string1.
 
  create object ui2_cl_json
    exporting
      data = ls_co.    " Data to serialize
 
  ui2_cl_json->serialize( ).
  lv_string2 = ui2_cl_json->get_data( ).
 
  mo_response->create_entity( )->set_string_data( iv_data = lv_string2 ).
 
  mo_response->set_header_field(
    exporting
      iv_name  =  'Content-Type'   " Header Name
      iv_value =  'application/json'   " Header Value
  ).

if_rest_resource~post redefinition:

  data: lv_response type string,
        lv_data type string,
		ls_com type zrest_tab,
        lt_co type standard table of zrest_tab with default key,
        ui2d_cl_json type ref to cl_trex_json_deserializer,
        ui2s_cl_json type ref to cl_trex_json_serializer,
        lo_entity type ref to if_rest_entity,
        lo_response type ref to if_rest_entity.
 
  lo_entity = mo_request->get_entity( ).
  lo_response = mo_response->create_entity( ).
 
  "read string data i.e json
  lv_data = lo_entity->get_string_data( ).
 
  create object ui2d_cl_json.
  ui2d_cl_json->deserialize( exporting json = lv_data importing abap = lt_co ).
  read table lt_co into ls_com with key mandt = 800.
 
  call function 'NUMBER_GET_NEXT'
    exporting
      nr_range_nr             = '01'
      object                  = 'ZRESTC'
      quantity                = '1'
    importing
      number                  = ls_com-id
    exceptions
      ...    .
  ...
 
  ls_com-createdby    = sy-uname.
  ls_com-createdon    = sy-datum.
  ls_com-time         = sy-uzeit.
 
  insert into zrest_tab values ls_com.
 
  create object ui2s_cl_json
    exporting
      data = lt_co.    " Data to serialize
 
  ui2s_cl_json->serialize( ).
 
  lv_response = ui2s_cl_json->get_data( ).
 
  lo_response->set_string_data( iv_data = lv_response ).
endmethod.

5. SICF neuer Service /sap/bc/rest/webco – HandlerList:ZCL_WEBCO_RHANDLER
6. (Test) SoapUI https://sapgw.sys100.sap.sd:8000/sap/bc/rest/webco (x-csrf-Token?)

Die Tabelle ZREST_TAB enthält jetzt die neue Zeilen.

Schreibe einen Kommentar



All fields are required. Your email address will not be published.