Introduction

The {z}restapi is an alternative solution to simplify the development of REST APIs on top of the Internet Communication Framework (ICF).

By separating what changes from what stays the same, it helps you to concentrate your effort on the business logic instead of spending your time developing repetitive code.

Features

  • Resource routing – resources are automatically identified by the request URL and HTTP method. The simple implementation of one (or all) of the 4 interfaces (CRUD methods) used by the {z}restapi is enough to make the resource available in your API.
  • Automatic value assignment – the values of form and querystring parameters sent in the HTTP request can be automatically assigned to the fields of the importing structure which have the same name (not case-sensitive). The dictionary structure to be used is easily defined by simply implementing one interface method.
  • Automatic conversion to JSON – the response data can be a dictionary structure, table or even a complex type. By default the response data is converted to the JSON format but you can easily add your own data conversion in the corresponding method of the {z}restapi handler.

Installation

  1. Install SAPLink – If you already have SAPLink installed in your system you can skip this step. We strongly recommend that you install also the SAPLink plugins.Click here to view the SAPLink wiki page.
  2. Install zJSON – If you already have zJSON installed in your system you can skip this step. Download the latest version of zJSON on GitHub and import the nugg file using the ZSAPLINK program. Don’t forget to activate the imported objects respecting the dependencies (dictionary objects first and classes second, in this case).

  3. Install {z}restapi – Download the latest version of {z}restapi on GitHub or ISinitial.org and import the nugg file NUGG_ZRESTAPI-x.x.x.nugg using the ZSAPLINK program. Don’t forget to activate the imported objects respecting the dependencies (dictionary objects first, interfaces and classes second and finally the reports).



Usage

NOTE: you can see a complete example of its usage on this blog post: AngularJS Single-Page Application + {z}restapi and token authentication

The first thing that you have to do is to create a new service in the ICF tree.

Go to the SICF transaction and create it under the SAP node like shown in figure 1.

http://isinitial.org/img/ICF.jpg

Figure 1 – Create the ICF service

On the Handler list tab inform the class ZCL_REST_HANDLER.

Optionally, for your first tests, you can go to the Logon Data tab and inform Client, User, Language and Password. This way you will not be prompted to inform your credentials.

Save, go back to the previous screen, right click the service and activate it.

http://www.isinitial.org/img/ICF2.jpg

Figure 2 – Activate the ICF service

Hello World example

Step 1 – Create the Resource

Go to transaction SE24 and create a new class

ZCL_REST_RESOURCE_HELLO_WORLD

The text that goes after the “ZCL_REST_RESOURCE_”, in this case “HELLO_WORLD”, defines the resource name.

Go to the Interfaces tab and inform the ZIF_REST_RESOURCE_READ interface as shown in the figure 3.


http://www.isinitial.org/img/HelloWorld1.jpg

Figure 3 – Implementing the interface



Step 2 – Implement the Interface READ method

Go to the Methods tab and implement the method ZIF_REST_RESOURCE_READ~READ with the code below.


METHOD zif_rest_resource_read~read.
  DATA: ls_response TYPE zst_rest_response_basic.
  ls_response-success = 'true'.
  ls_response-msg = 'Hello World'.
  ls_response-code = 200.
  e_response = ls_response.
ENDMETHOD.

Step 3 – Test your service

Copy the URL below and paste it into your browser’s address bar, replace the “host” and “port” with the hostname and port of your server and test your service.

host:port/sap/zrestapi/myApp/hello_world

If everything is OK you will receive the JSON response below.


{
  success: "true",
  code: 200,
  msg: "Hello World"
}


Retrieving parameters

Let’s extend our Hello World example to say Hello to everyone!

The form and/or querystring parameters sent in the HTTP request can be retrieved in two ways:

  1. They can be retrieved automatically by simply implementing the method GET_REQUEST_TYPE of the corresponding interface. All you have to do is to return the name of the dictionary structure to be used in the automatic value assignment. The values of the parameters are passed to the fields of the structure that have the same name. This assignment is not case-sensitive.

  2. Reading the internal table I_T_NVP that contains the Name/Value pairs of all form and querystring parameters of the HTTP request. This internal table is passed as an import pararameter to the CREATE / READ / UPDATE / DELETE methods of the corresponding interfaces.

Let’s change our Hello World example to retrieve a “NAME” parameter.

First we have to implement the GET_REQUEST_TYPE method to return the dictionary structure name. Create a structure on the ABAP Dictionary (SE11) with name ZST_HELLO_REQUEST. For our example this structure need to have only one field called “NAME” and type STRING.


Now let’s modify the READ method to receive the parameter and use it to compose the return message.



METHOD zif_rest_resource_read~read.
  DATA: ls_request  TYPE zst_hello_request,
             ls_response TYPE zst_rest_response_basic.
  ls_request = i_request.
  ls_response-success = 'true'.
  CONCATENATE 'Hello' ls_request-name
    INTO ls_response-msg
    SEPARATED BY space.
  ls_response-code  = 200.
  e_response = ls_response.
ENDMETHOD.


Now, let’s test our Hello World passing the “NAME” parameter.

/hello_world?name=Everyone!

If everything is Ok you will receive the JSON response below.


{
  success: "true",
  code: 200,
  msg: "Hello Everyone!"
}

Well, that’s all for now 🙂 . Over the next weeks I’ll be publishing other blog posts here talking about the usage of the {z}restapi inside BSP pages and about token based authentication for stateless services using the custom user authentication mechanism that is delivered along with the {z}restapi.



Download


www.isinitial.org


christianjianelli/zrestapi · GitHub


To report this post you need to login first.

Be the first to leave a comment

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

Leave a Reply