Skip to Content
Technical Articles
Author's profile photo Martin Grasshoff

How to Build your Mobile OData Service From Scratch

Hi,

In my last blog (here) I introduced the Mobile OData Service Generator, a feature in the Mobile Development Kit plugin for SAP Web IDE that generates a full fledged OData service for you based on a CSDL.

Well, I hope you are eager to hear how it works so we will jump directly into the action:

You need a place to store the data of your solution and to make it more easily to understand let’s assume you want to build a mobile canteen menu application for your peers.

In order to use the new Mobile OData Service Generator you need to active your Mobile Development Kit feature (which has recently been renamed to Mobile Services Development Tools) in the SAP WebIDE full-stack edition. (this video explains how it works)

Now you  can create a new project by choosing File > Project from Template. In the list of project templates, select Category “All categories” as a filter and select the “Mobile OData Service Project”.

For demonstration purposes we go the easiest way possible – choosing inMemory, so you can just type in a project name like “MyBackEnd” in the first step of the wizard dialog popping up.

You need to provide some more information about your service. Do you want to generate a service for CF or for NEO? Do you want to generate the persistence for SAP HANA, SAP ASE or Postgress? For our little getting started guide, we use InMemory with BasicAuth on NEO like below:

 

After finishing the project wizard, you’ll see a new project in your workspace like this:

This is the empty project structure of your service. Next step is to right click the project folder and create New > OData CSDL Document and fill out the form as follows:

These information are being used to generate the Java classes that will make your service.

In addition an file will be created that represents your OData Service: com.sap.mobile.backend.canteen.csdl.xml

If you don’t know what CSDL is all about, you should NOT read this. Keep focused on the task before you and save that link for later.

Just double click on the csdl.xml file to open a graphical editor that let’s you design your service.

I created a service like this:

This created a CSDL as follows:

<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="4.0" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx">
<edmx:DataServices>
<Schema Namespace="com.sap.mobile.backend.canteen" Alias="canteen" xmlns="http://docs.oasis-open.org/odata/ns/edm">

<EntityType Name="Canteen">
  <Key>
    <PropertyRef Name="CanteenID"/>
  </Key>
  <Property Name="CanteenID" Type="Edm.Int64" Nullable="false"/>
  <Property Name="Location" Type="Edm.String" Nullable="false" MaxLength="50"/>
  <Property Name="Name" Type="Edm.String" Nullable="false"/>
  <NavigationProperty Name="LunchMenu" Type="canteen.Menu" Nullable="false"/>
</EntityType>

<EntityType Name="Menu">
  <Key>
    <PropertyRef Name="MenuID"/>
  </Key>
  <Property Name="DateOfLunch" Type="Edm.Date" Nullable="false"/>
  <Property Name="Description" Type="Edm.String" Nullable="false" MaxLength="250"/>
  <Property Name="MenuID" Type="Edm.Int64" Nullable="false"/>
  <Property Name="Name" Type="Edm.String" Nullable="false" MaxLength="50"/>
  <Property Name="kcal" Type="Edm.Int32" Nullable="true"/>
  <Property Name="veggie" Type="Edm.Binary" Nullable="true"/>
</EntityType>

<EntityContainer Name="CanteenService">
  <EntitySet Name="CanteenSet" EntityType="canteen.Canteen">
    <NavigationPropertyBinding Path="LunchMenu" Target="MenuSet"/>
  </EntitySet>

  <EntitySet Name="MenuSet" EntityType="canteen.Menu"/>
</EntityContainer>

</Schema>
</edmx:DataServices>
</edmx:Edmx>

Right-click on the csdl.xml file and click “Generate Mobile OData Server” to start generation of an Java project that represents the service. After a while the “srv” folder should look like this:

The next step would be to compile the Java project, but before you can do that you need an SAP Cloud Foundry trial, then go back to SAP WebIDE preferences and add your CF account details as seen below.

Please click the “Install Builder” button.

Don’t forget to “Save” the changes in this dialog at the bottom of the screen!

Now we can compile the Java Project and we will get a odata-service-0.1.0.war file like this:

This concludes the service generation and the scope of the tool. Everything that follows is standard SAP Cloud Platform behavior.

Now we will export the war and bring it into our SAP Cloud Platform Cockpit and run it as a Java Application in our trial landscape.

So in your Cockpit, choose Java Applications, then “Deploy Application” button to upload our .war file.

I used these parameters:

 

After deploying the war, start the app.

Once the service started, you can assign the “ServiceUser” role to yourself and open the service in your browser under https://odataservicecanteed0XXXXXXtrial.hanatrial.ondemand.com/odata-service-0.1.0, where XXXXXX is your trial account name.

What you see is your freshly created service, which fully supports all OData operations (CRUD) as well as OData features like, filter, select. It even serves the data as OData v2 and v4 simultaneously, just like the client demands it!

 

/odata-service-0.1.0/CanteenSet

 

{"@odata.context":"https://odataservicecanteed055161trial.hanatrial.ondemand.com/odata-service-0.1.0/$metadata#CanteenSet","@odata.type":"#Collection(com.sap.mobile.backend.canteen.Canteen)","value":[]}

 

If you send along a header like this DataServiceVersion:2.0 you’ll get a result like this

{
  "d": {
    "results": []
  }
}

I admit that it looks quite complicated, but if you look at the steps I performed for achieving this, there was not a single line of code involved and I used a graphical modelling approach to create an OData service.

Next step would be to create either an mobile app with SAP Mobile Cards, SAP Cloud Platform SDK for iOS or SAP Cloud Platform SDK for Android on top of this OData service.

And the OData service generator can even do more, which I did not have covered so far:

  • Generate an delta token enabled OData Service
  • Provides hooks to inject your own logic into the service to make it even more versatile

But these topics will be covered by other blogs in the future. More information can be found in our docs.

I hope you enjoyed our newest addition to the SAP Cloud Platform Mobile Service feature set.

Have Fun,

Martin

 

Assigned tags

      17 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Dominique Pierre
      Dominique Pierre

      Hi Martin,

      Is this feature available in productive environments? We do have an account with the Mobile Services service and the MDK feature enabled but I can't see the Mobile OData Service Project template. I can see it in my trial account.

      Cheers,

      Pierre

      Author's profile photo Martin Lacasse
      Martin Lacasse

      Hi,

      This feature is not yet available in productive environments.  Stay tuned.

      thanks,

      Martin

      Author's profile photo Dominique Pierre
      Dominique Pierre

      Merci Martin.

      Do the generated OData services have default create, update and delete operations or do we have to implement them (and how)?

      Pierre

      Author's profile photo Martin Lacasse
      Martin Lacasse

      Bonjour Dominique,

      Yes we implement the default create, update and delete.  You can add code extensions to the generated code to add your own logic to create and updates (e.g. some business rules) or to implement actions/function imports.

      I would refer you to the official docs on how to extend the generated java code:

      https://help.sap.com/viewer/977416d43cd74bdc958289038749100e/Latest/en-US/dc55f74f6e064dd38ddc51f6ef79d11e.html

      Also, we allow delta enabling the service, without any custom code required.

      thanks,

      Martin

      Author's profile photo Mikael THEPAULT
      Mikael THEPAULT

      Hi Martin,

      Thank you for your blog.

      I followed your webinar 'Create Full-Fledged OData Service for Mobile Apps' on december 2018 and I have a question. You had initialized you Odata service with some data.

      For that you had modified the 'DataLoader.java' could you explain me how to do because I want to do the same thing with my hown Odata.

      Thank you for your help.

      Best regards,

      Mikaël.

      Author's profile photo Martin Grasshoff
      Martin Grasshoff
      Blog Post Author

      Hi Mikael,

      today, there is no need to load the data via coding anymore. You can provide a JSON file in your project to load the data for test and/or for initial data. That makes using this much easier. Here's the link to the docs

      Hope that helps.

      Regards,

      Martin

      Author's profile photo Seyed Ismail
      Seyed Ismail

      Hello,

       

      Great tutorial, first of all. But when I try to right click and generate mobile data service on CSDL file, I am getting error "(CSDL) No destination defined for OData service generator". What could be the reason?

       

      Thank you in advance for explaining.

       

      Seyed Ismail.

      Author's profile photo Himanshu Patel
      Himanshu Patel

      im also faxing the same issue

      Author's profile photo Ludo Noens
      Ludo Noens

      Please ensure you have Mobile Services enabled in the SAP Cloud Platform cockpit and the destination 'mobileservices' is defined.

      More information can be found in my blog here.

      Author's profile photo Don Menya
      Don Menya

      yes but how to connect postgresel actual database with sap ??? means i cound find the way that how to integreate postgreSQl with sap. @Martin Grasshoff guide me to connect postgresql database

      Author's profile photo ramdas biradar
      ramdas biradar

      Hi Martin,

      Thanks for this excellent tutorial, i have followed each step mentioned but when i deploy the project(.war)  on Neo Trial environment , i get HTTP 403 error.

      can you please help me resolve this issue.

       

      Author's profile photo Bryan Asuncion
      Bryan Asuncion

      Hi,

      Are there any tutorials on how to make the mobile OData server a pass through server, just getting the data and authorizations from another backend which has its own web apis? I saw there was an option for the server to not connect to any database so I assume that this option is for using at as a pass through server?

      Author's profile photo Ashok Yesala
      Ashok Yesala

      Hi Martin Grasshoff,

      Thank you for the blog,

      I did exactly same steps but when generating mobile odata service its getting failed. Please find screenshot and please do let me know what went wrong.

      Author's profile photo Ritesh Manktala
      Ritesh Manktala

      HI Martin/

       

      @martingrasshoff

       

      I am following up your opensap for mobile application development and the same example is there as well, in this when i try to call the service as per  below it works fine..

      e.g. (Works Fine)

      https://xxxxxxxxcanteenservice.cfapps.eu10.hana.ondemand.com/CanteenSet(1)

       

      but the below service call gives error and i am stuck here... unable to move forward for the mobile cards exercise..what could be the reason ?

      Gives Error

      https://xxxxxxxxcanteenservice.cfapps.eu10.hana.ondemand.com/CanteenSet(1).serveMenus

       

      <error xmlns="http://docs.oasis-open.org/odata/ns/metadata">
      <code>0</code>
      <message>DataQueryException: Unexpected query character: {U+002E}</message>
      <innererror>
      <details>com.sap.cloud.server.odata.DataQueryException: Unexpected query character: {U+002E}</details>
      </innererror>
      </error>
      I enabled the error logging/trace as well it shows the below there

       

      "written_at":"2020-08-20T07:06:45.954Z","written_ts":571025117341712,"tenant_id":"-","component_id":"6a7de0ae-36e8-4ad5-a01e-31efb1fd9193","DCComponent":"","component_name":"canteenService","organization_name":"xxxxxxxxxtrial","component_type":"application","space_name":"dev","component_instance":"0","organization_id":"3c00ac37-5254-4f53-b469-cd8fbfe69520","correlation_id":"-","CSNComponent":"","Application":"canteenService","space_id":"978c94cc-3396-4b6f-b86a-0703a0572823","container_id":"10.32.2.11","type":"log","logger":"sap.xs.odata.server","thread":"http-nio-0.0.0.0-8080-exec-1","level":"ERROR","categories":[],"msg":"[CanteenService] Error Content: <?xml version=\"1.0\" encoding=\"utf-8\"?>\n<error xmlns=\"http://docs.oasis-open.org/odata/ns/metadata\"><code>0</code><message>DataQueryException: Unexpected query character: {U+002E}</message><innererror><details>com.sap.cloud.server.odata.DataQueryException: Unexpected query character: {U+002E}</details></innererror></error>" }

       

      thank you

      Ritesh

      Author's profile photo Martin Grasshoff
      Martin Grasshoff
      Blog Post Author

      Hi Ritesh,

      try CanteenSet(1)/serveMenus instead of "."

      -Martin

      Author's profile photo Ritesh Manktala
      Ritesh Manktala

      Hi Martin,

      Thank you ..it worked.. i somehow was always trying with a dot (.).

      I wanted to figure out why this URL never worked in Mobile cards. ..hence started looking at API in browser and now it came as a Case Sensitive keyword. While defining it in CSDL i had taken it as ServeMenus.

      thank you once again.

      Cheers!!

      Ritesh

       

      Author's profile photo Francisco José Carrasco Valverde
      Francisco José Carrasco Valverde

      Hello. I got the error "(CSDL) No destination defined for OData service generator" when "Generate Mobile odata Service".

      I've already checked the destination and mobil service instance "Mobile Service" is created.

      Thanks