Sie sind auf Seite 1von 19

Understanding OData in 6 steps

OData (Open Data Protocol) is an OASIS standard that defines the best practice for
building and consuming RESTful APIs. OData helps you focus on your business logic
while building RESTful APIs without having to worry about the approaches to define
request and response headers, status codes, HTTP methods, URL conventions, media
types, payload formats and query options etc. OData also guides you about tracking
changes, defining functions/actions for reusable procedures and sending
asynchronous/batch requests etc. Additionally, OData provides facility for extension to
fulfil any custom needs of your RESTful APIs.
OData RESTful APIs are easy to consume. The OData metadata, a machine-readable
description of the data model of the APIs, enables the creation of powerful generic client
proxies and tools. Some of them can help you interact with OData even without knowing
anything about the protocol. The following 6 steps demonstrate 6 interesting scenarios
of OData consumption across different programming platforms. But if you are a nondeveloper and would like to simply play with OData, XOData is the best start for you.

Building your first simple Gateway


service
I imagine youre reading this because youve either just
installed SAP NetWeaver Gateway, or are considering
installing it. Well Im here to tell you that, from where Im
standing, it looks like the future of SAP will rely heavily
upon it, so youre making the right steps!
1. Purpose
SAP NetWeaver Gateway is an enabling component of SAP which allows you to
expose data from one or many systems to the outside world. The underlying
systems are usually SAP ones (SAP ECC, SAP CRM, SAP ERP and so on. ) but it is
important to note that you can use Gateway with non-SAP systems too.

Now you all have a basic understanding of what Gateway is, youre ready to get on
with the fun bit!

Pre-requisites for this exercise

SAP system (ERP / ECC / CRM)

SAP NetWeaver Gateway Release 2.0 Support Package 4 or above

Free Gateway trial download here


Some basic understanding of ABAP and databases
In this document I will use SAP ERP with integrated Gateway, i.e. both on the same
box. But I will explain as I go along how this would differ if your Gateway instance is
separate.
The data I will expose will be SFlight data, both the Flights (SFLIGHT) and Carrier
(SCARR) database tables. SFlight is a standard set of tables and data which comes
free with SAP systems. To find out more about SFlight please visit this post.

2. Create a new project using the Service Builder


Login to ERP and navigate to transaction SEGW

Click on Create Project

Chose a project name, give your project a description and select Standard
generation strategy.
In my example I will be creating the service as a local object.

2.1 Create an entity Carrier from table SCARR


Right click on Data Model, and select Import > DDIC Structure.

Enter the name of the ABAP Dictionary Structure you want to use, and hit enter. In
our example we are using SCARR.

Enter an Object Name, this will be the name of your entity. Select any fields to not
include (ignore) and ensure the correct key is selected.

You can now change the names of any of the properties.

2.2 Create an entity set Carriers


Double click on Entity Sets on the left hand side, and chose Append Row. Enter
an entity set name usually a pluralisation of the Entity name, which in our

example is Carriers.

Chose the appropriate entity on which to base this entity set

2.3 Generate the Service


In order to use the service with the changes you have made you will first need to
generate it. This will create the underlying ABAP classes for you, containing all the
information youve specified thus far in the service builder. Anytime you make a
change in the service builder you will need to generate again, to ensure the ABAP
classes are up-to date.
Select the project name that you wish to generate, and click the generate wheel.

Enter names which you want the underlying classes to have.

It is best practice for the model provider class name to end in _MPC and the data
provider class to end in _DPC. The extension classes of these should be named the
same as their corresponding base class, with the additional suffix of _EXT.
Generating can take a few moments, so dont worry if it isnt instant. Once it is
complete you will see a detailed log of successes, warnings and errors. If you have
done everything right this should be all green.

3. Activate the new Service


In order to be able to use your service you will first have to activate it. This means
adding it to a list of services which can be reached through Gateway. Because I am
using integrated Gateway this is done in the same system, however for those of you
using a separate Gateway instance, login to you Gateway server first.
Navigate to transaction /IWFND/MAINT_SERVICE

Click Add Service


Because my Gateway instance is on the same box as my ERP, I enter LOCAL as the
system alias. To learn more about setting up system aliases you can read this
document.
Enter the name of your service and hit enter.

Select your service from the list.

Hit save and navigate back to the Activate and Maintain Services screen. Scroll
down the list of services to find your one.

4. Test the Service from a browser

We now want to see if our service is accessible from the web, to validate whether
we have set it up correctly.
Click the Call Browser button in the bottom left section of the screen.
If your call was successful you will see something like this:

Now we can modify the URL so the suffix is $metadata?sap-ds-debug=true

Here we can see our Carrier entity, with its properties, and the fact that there is an
entity set associated with this entity.
If we change the URL again to end in /Carriers?sap-ds-debug=true we will see this
error message:

This is because we havent yet told our service how to read Carrier information from
the SCARR database table.

5. Add ABAP to populate the Carriers


Navigate back to the Gateway Service Builder, SEGW.
Expand Service Implementation > Carriers
Right click on Get EntitySet (Query) and select Go to ABAP Workbench

Click the tick on the warning message

This then takes you into the ABAP Class where you will need to build your
implementation.
Click on display object list

Find the CARRIERS_GET)ENTITYSET method of the left hand side, right click on it,
and chose Redefine.

Enter this code into the newly redefined method:


select * from scarr into corresponding fields of table et_entityset.
Then hit activate.

6. Test the Service from a browser


Return to your browser and refresh the page.

As you can see we now get a stream of information back, detailing all the Carriers
stored in the SCARR table in ERP.

7. Add another entity and link the two together


In this section we will create another entity set for Flights, and create a link between
carriers and flights.

One airline carrier has many flights, but each flight has only one airline carrier.

7.1 Create an entity Flight with corresponding entity set


from table SFLIGHT
Repeat the steps above to create a new entity Flight, and corresponding entity set
Flights. This time, instead of basing it on the structure SCARR we will use the
structure SFLIGHT.

Dont forget to hit Generate when youre done!

7.2 Add ABAP to populate the Flights


Navigate to the ABAP workbench for the Flight entity.
Find the method FLIGHTS_GET_ENTITYSET, and redefine it.
Enter ABAP to select everything from the flights table.

Activate the code.


7.3 Test the Service from a Browser, to ensure the Flights
entity has been added
Use the suffix Flights?sap-ds-debug=true on your URL to get back all the flights

8. Create a navigation from Carrier to Flights


We want to be able to go from a specific carrier, to all the flights that carrier has. To
do this we need to create a navigation in the service builder.
First we create a new Association:

Then we create the navigation:

Generate.
Now we need to modify the ABAP so that when we ask for a list of flights for a
specific carrier we dont get back all the flights, we just get back those relevant to
us.
Navigate to the ABAP method FLIGHTS_GET_ENTITYSET.
Change the code to read:
data: ls_key like line of it_key_tab.
read table it_key_tab into ls_key with key name = 'Carrid'.
* If there is a carrier ID specified, use it to select only those for the specific
carrier
if sy-subrc eq 0.
select * from sflight into corresponding fields of table et_entityset
where carrid = ls_key-value.
* If there is no carrier specified, read all the flights
else.
select * from sflight into corresponding fields of table et_entityset.
endif.

IT_KEY_TAB is importing parameter of the method which will hold all keys which
have been passed in. In our example it will contain a carrier ID f the navigation is
from Carrier, because Carrid if the key of the Carrier entity.

9. Test the Service from a browser


Use the URL suffix Carriers('AA')/Flights?sap-ds-debug=true to test whether your
implementation has worked. If it has the list of results will only show flights where
the carrier ID is AA.

10. Wrap up
Congratulations, youve just built your first, easy Gateway service. Obviously this
post does not go into the complexities of building a Gateway service, but I hope it
has given you an idea about where to begin.
For some interesting insights around, and beyond this topic its worth checking out
the following:

Das könnte Ihnen auch gefallen