Saturday, May 11, 2024

Creation of RESTful Webservice in SAP

 

What is REST ful web service?

It’s a named resource which can be accessed using the URL containing the ID (In our case Sales order number ) of the resource in distributed environment.


Step1: Create a handler class which is having the business logic to get the sales order details

           Go to se24 enter the class name



Step 2: Now we need to implement the interface IF_HTTP_EXTENSION



You can see the method HANDLE_REQUEST from the interface IF_HTTP_EXTENSION.We will use this method in future to implement business logic.



Step 3:Write below code in the HANDLE_REQUEST method




Source code:

method IF_HTTP_EXTENSION~HANDLE_REQUEST.

  data:lv_path type string,

       lv_data type string,

       lt_data type table of string.

  types:begin of ty_head,

        VBELN type vbak-VBELN,

        KUNNR type vbak-KUNNR,

        VKORG type vbak-VKORG,

        end of ty_head.

  data:ls_head type ty_head,

       lt_list type table of BAPIORDERS,

       ls_list type BAPIORDERS,

       XML_OUT type string.

* get the request attributes

  lv_path = server->REQUEST->get_header_field( name = ‘~PATH_INFO’ ).

  SHIFT lv_path LEFT BY 1 PLACES.

* Get the sales order details

  select single vbeln

                kunnr

                vkorg

           into ls_head

           from vbak

          where vbeln = lv_path.

* Convert the data to  XML format

  CALL TRANSFORMATION ID

               SOURCE TAB = ls_head

           RESULT XML XML_OUT

              OPTIONS XML_HEADER = ‘WITHOUT_ENCODING’.

* Set the converted data to Browser

  server->response->set_cdata( data = XML_OUT ).

endmethod.

Step4: Go to transaction SICF and click on Execute button



Step 5: Create a child node under the services hierarchy by right click on the node. I have created services in the following path

Default host->SAP->bc



Step 6: Enter the service name click to continue



Added service will be under the respective hierarchy as below



Step 7: Double click on the service zrest_service then below screen will appear



Step 8: Go to Handler list tab and give the handler name which we created earlier as ZCL_SALES_ORDER_HANDLER



Step 9: Save and activate the service as below



Step 10: Now test the service



Step 11:Give sales order number in URL and click F5



Note: To debug the web service you have to put external break point in the method HANDLE_REQUEST

No comments:

Post a Comment