Skip to Content

Building RESTful webservices for ABAP still seems to be an issue for SAP Netweaver. In fact, it isn’t. The great article “

Building Mash-Ups and simplifying Application Integration withRESTful Web Services in SAP ABAP

” shows how to implement basic RESTful functionality in ABAP. Let’s take this concept further. 

Hint

All development objects mentioned in this tutorial are available as SAPLink nugget. To avoid inconsistencies while activating, please first activate ZIF_RESOURCE_CONTROLLER and ZRESOURCE_ROUTNG in order to activate any other development objects shipped with this nugget.If you haven’t heard about REST before, you should read this

article

in order to get familiar with the REST paradigm. Please note, the sources for this example are available as a

SAPLink nugget

.

What do we miss?

A good introduction to the REST paradigms is explained in the article. There are some restrictions with this approach. I would like to explain to you, what restrictions are existent and how to bypass them. First of all: For RESTful webservices, a hierarchy of resources would be needed. This means, in terms of the SAP flight model shipped with all WebAS ABAP releases, there should be flight connections (sub-resources) available for certain carriers (super-resources). For example, when performing “GET” requests using a standard browser…

http://<yourServer>/resources/scarr/LH/ returns details of the carrier Lufthansa…0.1.

http://<yourServer>/resources/scarr/LH/SPFLI/* returns all flight connections related to Lufthansa…0.1.

http://<yourServer>/resources/scarr/LH/SPFLI/402 returns the specific flight connection “402” related to Lufthansa.  Second, there should be some kind of delegation logic for our endpoint handler class which is dealing with all requests sent to a specific endpoint. This means, we only want to use one single ICF handler which should be responsible for delegating the requests to another resource handler class according to some routing logic. Parameters passed in the URL, like “LH” and “402” serve as “input parameters” for the methods invoked from this class. The parameters should be handed over in a structured way.

Let’s start

  First, we need to create an endpoint handler for an URL on the Application server.

Call transaction SICF and press F8.

!https://weblogs.sdn.sap.com/weblogs/images/251763241/RA_001.png|height=300|alt=Transaction SICF|width=500|src=https://weblogs.sdn.sap.com/weblogs/images/251763241/RA_001.png|border=0!!https://weblogs.sdn.sap.com/weblogs/images/251763241/RA_001.png|height=1|alt=|width=1|src=https://weblogs.sdn.sap.com/weblogs/images/251763241/RA_001.png|border=0!     

In the tree bar, right click on the “sap” node and choose “New Sub-Element”

!https://weblogs.sdn.sap.com/weblogs/images/251763241/RA_002.png|height=400|alt=Create new independent Service|width=500|src=https://weblogs.sdn.sap.com/weblogs/images/251763241/RA_002.png|border=0

Choose a name for the new independent service. In the example, this endpoint is called “resources”. 

In the maintenance screen, go to tab “Handler List”. Type in the class name of the class which is handling requests sent to this node. In our case, there is already a class which is named ZCL_RESTFUL_CONTROLLER shipped with the nugget file. This class is implementing the Interface IF_HTTP_EXTENSION, since this is needed to register a handler class for a node represented by an URI.

(Please note, the sources for this example are available as a

SAPLink nugget

!https://weblogs.sdn.sap.com/weblogs/images/251763241/RA_010.png|height=450|alt=Define handler|width=600|src=https://weblogs.sdn.sap.com/weblogs/images/251763241/RA_010.png|border=0!</body>

To report this post you need to login first.

6 Comments

You must be Logged on to comment or reply to a post.

  1. Hi,

    if you face any problems while importing the nugget, feel free to contact me.

    I recognized, that the ZIF_RESOURCE_HANDLER~HANDLE_GET implementations might be missing in classes ZCL_RESOURCE_SCARR and ZCL_RESOURCE_SPFLI. In this case it is needed to import the nugget using SAPLink and activate only the Interface ZIF_RESOURCE_HANDLER in the first iteration (to see the interface object in your local objects, it might be needed to Rebuild the object list in your $TMP package).

    Finally, import the nugget using SAPLink again and choose the option “Overwrite originals”.
    Now you can activate all the new classes and dictionary objects at once. You should see the implementations of method ZIF_RESOURCE_HANDLER~HANDLE_GET in both ZCL_RESOURCE_SCARR and ZCL_RESOURCE_SPFLI

    Regards,

    Uwe

    (0) 
  2. Aditya Palekar
    Hello Uwe,

    I am trying to implement a REST service which contains multiple parameters. The URL that I invoked is
    http://inst50062303.blr4.sap.corp:8000/sap/resources/compcode/0001/customer/0000100000/datefrom/19900101/dateto/20102106/noteditems/X?sap-client=001

    There are 5 parameters: Company Code, Customer, Date From, Date To and NotedItems.

    There is 1 service created under SICF–>Default/SAP called “resources”

    I believe I am doing something wrong here? Should I set up a hierarchy of resources to handle multiple parameters?

    Please advise

    Regards
    Aditya

    (0) 
    1. Hi Aditya,

      You would have to perform the following steps:

      1. implement a single ICF node with handler class ZCL_RESTFUL_CONTROLLER
      2. create class, e.g.

      ZCL_RESOURCE_CUSTOMER

        implementing

      ZIF_RESOURCE_HANDLER

      3. write a new routing entry in class ZCL_RESTFUL_CONTROLLER, e.g.

      method GET_ROUTING.
        DATA ls_routing LIKE LINE OF rt_routing_tab.

        ls_routing-clnt = sy-mandt.
        ls_routing-guid = ‘123’.
        ls_routing-url_info = ‘/scarr/{PARAM_CARR_ID}’.
        ls_routing-handler_class = ‘ZCL_RESOURCE_SCARR’.
        APPEND ls_routing TO rt_routing_tab.

        ls_routing-clnt = sy-mandt.
        ls_routing-guid = ‘456’.
        ls_routing-url_info = ‘/scarr/{PARAM_CARR_ID}/spfli/{PARAM_CONNID}’.
        ls_routing-handler_class = ‘ZCL_RESOURCE_SPFLI’.
        APPEND ls_routing TO rt_routing_tab.

        ls_routing-clnt = sy-mandt.
        ls_routing-guid = ‘456’.
        ls_routing-url_info = ‘/scarr/{PARAM_CARR_ID}/spfli/{PARAM_CONNID}’.
        ls_routing-handler_class = ‘ZCL_RESOURCE_CUSTOMER’.
        APPEND ls_routing TO rt_routing_tab.

        ls_routing-clnt = sy-mandt.
        ls_routing-guid = ‘6789’.
        ls_routing-url_info = ‘/compcode/{COMPCODE}/customer/{CUSTOMER}/datefrom/{DATEFROM}/dateto/{DATETO}/noteditems/{NOTEDITEMS}?sap-client=001’.
        ls_routing-handler_class = ‘ZCL_RESOURCE_CUSTOMER’.
        APPEND ls_routing TO rt_routing_tab.

      endmethod.

      Your request below will be delegated to the corresponding method HANDLE_GET, HANDLE_POST etc. in class

      ZCL_RESOURCE_CUSTOMER

      check IT_PARAMS for the extracted five URL Parameters. In case it is not working – just send me an EMail, it might indicate a bug.

      Regards,
      Uwe

      (0) 
  3. Pierre Dominique
    Hi,

    Uwe, there’s a bug in the GET_CONTROLLER method. Instead of using SHIFT to remove the pattern from the URL, REPLACE must be used.

    Replace the following code :
    SHIFT lv_url_info LEFT DELETING LEADING lv_url_part.

    With :
    REPLACE FIRST OCCURRENCE OF lv_url_part IN lv_url_info WITH SPACE.

    Regards,

    Pierre

    (0) 

Leave a Reply