This blog post is a continuation of a series of tree posts, about how to create and consume gateway service, the first one and three can be found here:
How to create and consume a gateway service via SAPUI5 - Part 1
How to create and consume gateway service via SAPUI5 – Part 3
All the prerequisites are listed in previews article, please read the first part before you continue.
In this second part, we will see how to create an OData service that will be used in the SAPUI5 application.
Let´s start...
Creating OData Service
To create the Odata service we need to access the SEGW transaction, and proceed with a few steps:
- Access SEGW transaction, you will enter Sap Netweaver Gateway Service Builder.
- Start by creating a new project, to do this, click in “New Project”.
- In "Create Project" window, enter the project name “ZHelloOData”, description “Hello Odata Service” and inform your Z package under Package.
- After creating the project, your project structure will look like the one in the image below.
- Next you need to create a service based in the SCARR table. To do this, right click on “Data Model” folder, choose Import->DDIC Structure.
- A wizard window will be displayed, in the first step, enter the Name “SCARR” and in ABAP Structure, enter SCARR again. Check if “Create Default Entity Set” is marked. When done, click on the Next button.
- In the next step, mark all of the checkboxes and then click in Next again.
- In the last step, choose the key fields, according to the image below, and then click in Finish.
- After these tree steps, the project will look like that:
- Now let´s generate the Runtime objects. Click in the button marked below.
- The popup window for choosing the appropriate request will be displayed, select the same request which we used in the first tutorial, or create a new one if you prefer.
- In the next step, it will be displayed the "Model and Service Definition" window, keep all of the fields as it is suggested and confirm.
- Then, choose your Z package and a request again.
- At the end of these steps, the "Runtimes Artifacts" will be generated. See the image below and confirm if everything is OK.
Registering and testing the Service
Now we are going to register the service and make a first test. Please proceed with the following steps:
- Double click on the "Service Maintenance" node, and then click "Register". See the image below.
- In the "Add Service" window, we have to keep all the values as suggested, entering only the package name. Then click "Continue". Check the image below.
- Next inform your previously created request.
- If everything is OK you will have a green light in "Registration Status".
- With everything OK, you can go to "Gateway Client" and test the recently created service. Click in "Gateway Client".
- A new window will be displayed. To test, click on the "Execute" button and then check if the HTTP response has the code 200. Confirm the link to your service, you will need this link in the third part of this article. This link is used to access our recently created service.
- You can also check the results in your browser, entering the URL marked below.
Developing the Service Implementation
- Now we are going to develop the service to query the results of the SCARR table. Let’s return to Sap Netweaver Service Builder (SEGW).
- First, open the "Service Implementation" tree structure. Then, right click on "GetEntitySet (Query)" and choose “Go to ABAP Workbench”. See the example in the image below.
- In the popup click in Yes, then confirm the creation of the implementation.
- The ABAP workbench will be displayed, then open the SCARRSET_GET_ENTITYSET method. Then, confirm the creation of the method.
- Now let´s implement the code to select data from SCARR table. See the image below, and activate the objects.
- For a final test, open the Gateway Client again, click in execute and check the name of the collection created. You can also do this, informing the URL in you browser.
- With the collection name, we need to change the URL to test if the results will be displayed.
Well, we finish the second part here, in the last part of this series we are going to consume the service created in this article in a SAPUI5 application.
See you in the next post!
Regards, Balbino.