ABAP Blog Posts
cancel
Showing results for 
Search instead for 
Did you mean: 
471

Step by step process for creating a rest API (Web Service) in sap, which will be consume by
web based application:

1. Create Table- ZDT_WEB_COMPLAIN

Srikanta_Kumar_Gouda_0-1763041199392.png

2. Create SNRO Number range object: ZSNRO_WEB

Transaction code: SNRO

Give SNRO name and click on create 

Srikanta_Kumar_Gouda_2-1763041367838.png

enter short text, long text, number length domain, % warning and save.
Srikanta_Kumar_Gouda_1-1763041267557.png

click on Ranges--> click on change intervals--> click on new entries.

insert the number range and save.

Srikanta_Kumar_Gouda_3-1763041516000.png

3. Create request handler class : ZCL_WEBCOMPLAINT_REQHANDLR

Go to Transaction Code - SE24 and create request handler class.

Srikanta_Kumar_Gouda_4-1763041612509.png

Srikanta_Kumar_Gouda_5-1763041700880.png

enter the superclass as cl_rest_http_handler and click on save.

Srikanta_Kumar_Gouda_6-1763041718291.png

Go to method section to see super class methods.

Srikanta_Kumar_Gouda_7-1763041805586.png

select the get_root_handler and click on redefine. Add the below code in between method and endmethod.

Srikanta_Kumar_Gouda_8-1763041875753.png

DATA(lo_router) = NEW cl_rest_router( ).  
  
    lo_router->attach( EXPORTING  
                       iv_template =  '/webcomplaint' " Unified Name for Reso  
                       iv_handler_class =  'ZCL_WEBCOMPLAINT_REQHANDLR'   " Object Type Name  
                      "  it_parameter  = " Resource contructor parameters  

Srikanta_Kumar_Gouda_10-1763041998349.png

save and activate the class .

Srikanta_Kumar_Gouda_9-1763041913576.png

4. Create resource provider class: ZCL_WEBCOMPLAINT_REQPROVIDER.

Go to Transaction Code - SE24 and create request provider class.

Srikanta_Kumar_Gouda_3-1763043027077.png

 

Srikanta_Kumar_Gouda_0-1763042941293.png

enter the superclass as  cl_rest_resource  and click on save.

Srikanta_Kumar_Gouda_4-1763043198086.png

Go to method section to see super class methods.

Srikanta_Kumar_Gouda_5-1763043230879.png

select the method 'GET' and click on redefine . Add the below code.

DATA : lv_string1   TYPE vbeln,         "string, 
           lv_string2   TYPE string, 
           gs_complaint TYPE zdt_web_complain. 
 
    lv_string1 = mo_request->get_uri_query_parameter( iv_name = 'DOC_ID' ). 
 
    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT' 
      EXPORTING 
        input  = lv_string1 
      IMPORTING 
        output = lv_string1. 
 
 
    SELECT SINGLE * FROM zdt_web_complain INTO CORRESPONDING FIELDS OF gs_complaint 
    WHERE doc_id = lv_string1. 
 
 
    /ui2/cl_json=>serialize( 
      EXPORTING 
        data        =  gs_complaint    " Data to serialize 
*       COMPRESS    = ABAP_FALSE    " Skip empty elements 
*       NAME        =     " Object name 
*       PRETTY_NAME =     " Pretty Print property names 
*       TYPE_DESCR  =     " Data descriptor 
      RECEIVING 
        r_json      =   lv_string2  " JSON string 
    ). 
 
    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 
    ). 



save and activate .  

similarly redefine the post method and add the below code. 

  METHOD if_rest_resource~post.
*CALL METHOD SUPER->IF_REST_RESOURCE~POST
*  EXPORTING
*    IO_ENTITY =
*    .

    DATA : lv_string1   TYPE vbeln,         "string,
           lv_string2   TYPE string,
           lv_response  TYPE string,
           gs_complaint TYPE zdt_web_complain.

    DATA(lo_entity)    = mo_request->get_entity( ).
    DATA(lo_response)  = mo_response->create_entity( ).

    "read string data i.e json
    DATA(lv_data) = lo_entity->get_string_data( ).

    /ui2/cl_json=>deserialize(
      EXPORTING
        json                  =  lv_data   " JSON string
*     PRETTY_NAME           =     " Pretty Print property names
      CHANGING
        data                  =  gs_complaint   " Data to serialize
    ).
*   CATCH CX_SY_MOVE_CAST_ERROR.    "

    CALL FUNCTION 'NUMBER_GET_NEXT'
      EXPORTING
        nr_range_nr             = '01'
        object                  = 'ZSNRO_WEB'
        quantity                = '1'
*       SUBOBJECT               = ' '
*       TOYEAR                  = '0000'
*       IGNORE_BUFFER           = ' '
      IMPORTING
        number                  = gs_complaint-doc_id
*       QUANTITY                =
*       RETURNCODE              =
      EXCEPTIONS
        interval_not_found      = 1
        number_range_not_intern = 2
        object_not_found        = 3
        quantity_is_0           = 4
        quantity_is_not_1       = 5
        interval_overflow       = 6
        buffer_overflow         = 7
        OTHERS                  = 8.
    IF sy-subrc <> 0.
* Implement suitable error handling here
    ENDIF.

    gs_complaint-createdby    = sy-uname.
    gs_complaint-createdon    = sy-datum.
    gs_complaint-time         = sy-uzeit.

    INSERT INTO zdt_web_complain VALUES gs_complaint.


    /ui2/cl_json=>serialize(
      EXPORTING
        data        =   gs_complaint    " Data to serialize
*        COMPRESS    = ABAP_FALSE    " Skip empty elements
*        NAME        =     " Object name
*        PRETTY_NAME =     " Pretty Print property names
*        TYPE_DESCR  =     " Data descriptor

      RECEIVING
          r_json   = lv_response
          ).

    " JSON string
    lo_response->set_string_data( iv_data = lv_response ).

ENDMETHOD.

 

save and activate.  

similarly redefine the post method and add the below code. 

METHOD if_rest_resource~post.
*CALL METHOD SUPER->IF_REST_RESOURCE~POST
*  EXPORTING
*    IO_ENTITY =
*    .

    DATA : lv_string1   TYPE vbeln,         "string,
           lv_string2   TYPE string,
           lv_response  TYPE string,
           gs_complaint TYPE zdt_web_complain.

    DATA(lo_entity)    = mo_request->get_entity( ).
    DATA(lo_response)  = mo_response->create_entity( ).

    "read string data i.e json
    DATA(lv_data) = lo_entity->get_string_data( ).

    /ui2/cl_json=>deserialize(
      EXPORTING
        json                  =  lv_data   " JSON string
*     PRETTY_NAME           =     " Pretty Print property names
      CHANGING
        data                  =  gs_complaint   " Data to serialize
    ).
*   CATCH CX_SY_MOVE_CAST_ERROR.    "

    CALL FUNCTION 'NUMBER_GET_NEXT'
      EXPORTING
        nr_range_nr             = '01'
        object                  = 'ZSNRO_WEB'
        quantity                = '1'
*       SUBOBJECT               = ' '
*       TOYEAR                  = '0000'
*       IGNORE_BUFFER           = ' '
      IMPORTING
        number                  = gs_complaint-doc_id
*       QUANTITY                =
*       RETURNCODE              =
      EXCEPTIONS
        interval_not_found      = 1
        number_range_not_intern = 2
        object_not_found        = 3
        quantity_is_0           = 4
        quantity_is_not_1       = 5
        interval_overflow       = 6
        buffer_overflow         = 7
        OTHERS                  = 8.
    IF sy-subrc <> 0.
* Implement suitable error handling here
    ENDIF.

    gs_complaint-createdby    = sy-uname.
    gs_complaint-createdon    = sy-datum.
    gs_complaint-time         = sy-uzeit.

    INSERT INTO zdt_web_complain VALUES gs_complaint.


    /ui2/cl_json=>serialize(
      EXPORTING
        data        =   gs_complaint    " Data to serialize
*        COMPRESS    = ABAP_FALSE    " Skip empty elements
*        NAME        =     " Object name
*        PRETTY_NAME =     " Pretty Print property names
*        TYPE_DESCR  =     " Data descriptor

      RECEIVING
          r_json   = lv_response
          ).

    " JSON string
    lo_response->set_string_data( iv_data = lv_response ).

ENDMETHOD.

save and activate.

5. Create SICF service:

Execute Transaction code SICF.

Srikanta_Kumar_Gouda_6-1763043575157.png

click on execute. 

Srikanta_Kumar_Gouda_7-1763043591676.png

 

give the service element name and press enter. 

Srikanta_Kumar_Gouda_9-1763043616789.png

enter description and request handler class name [ zcl_webcomplaint_reqhandlr  ]and save. 

Srikanta_Kumar_Gouda_10-1763043799234.png

 

Srikanta_Kumar_Gouda_11-1763043895392.png

on SICF main screen activate the service.

Srikanta_Kumar_Gouda_12-1763043944346.png

now before testing add some record in table, then test the service.

Srikanta_Kumar_Gouda_13-1763043960343.png

 

Srikanta_Kumar_Gouda_0-1764241570181.png

4 Comments