As you all aware of that creation of SOA (System oriented architecture) web service using FM. Now let’s create a REST(Representational state transfer) web service which is getting much attention now a days.
Recently I have been given with a requirement to create a REST( Representational state transfer) web service which returns the sales order details in XML format.
Business requirement: Consumer want to get the details of sales order from the SAP by passing the sales order number as ID through URL.
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.
With the help of SCN and Wiki documents I have created Web service as below.
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
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
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 ).
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
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