Creating OData V4 Services using SAP Cloud Platform SDK for Service Development
This blog post will take you through a step-by-step process to build OData V4 services using the SAP Cloud Platform SDK for Service Development. As a result of following this post, you will be able to:
- Build first project exposing OData V4 service
- Create an account on SAP Cloud Platform and Deploy application
- Execute Service on SAP Cloud Platform
Prerequisites
To start with the SAP Cloud Platform SDK for Service Development, you need the following set of pre-requisites installed on your machine:
- JDK Version 8
- Apache Maven (https://maven.apache.org/install.html)
- An IDE of your choice – We use Eclipse IDE for JEE Developers for our blog here
SAP Cloud Platform SDK for Service Development components are available at Maven Central and you should point to Maven Central to get the maven archetypes used for building the projects. To do this, you need to ensure the below 2 points.
- Maintain Maven Central Repository in the settings.xml at ${user.home}/.m2/settings.xml. You can do this by adding the repository pointing to http://repo1.maven.org/maven2/ in the settings.xml file.
- On Eclipse, you should maintain Preferences –> Maven –> Archetypes. You can add the archetype catalog from Maven Central by clicking on “Add Remote Catalog” and pointing to https://repo1.maven.org/maven2/archetype-catalog.xml .
Create Project
Create a Maven Project using the archetype with the following details:
Group Id | com.sap.cloud.servicesdk.archetypes |
Artifact Id | cloud-servicesdk-odatav4-quickstart |
Version | 1.1.3 |
On Eclipse, you can do this by following the menu option File –> New –> Maven –> Maven Project. Use the archetype details in the table above on the next screen. Please see images below.
On the next screen, specify any Group ID, Artifact ID and version that you would like to use for the project that you will create. You can also specify the package names for your classes where you will implement the service implementation.
The relevance of the property “hdi-container-service” will be explained in a later blog post. You can ignore this value for this step.
Your project is ready for implementation now.
Sample Project
At this point, in case you are interested to see how an actual implementation may look like, you can do this by creating a project using the archetype with the below details.
Group Id | com.sap.cloud.servicesdk.archetypes |
Artifact Id | cloud-servicesdk-odatav4-sample |
Version | 1.1.3 |
The intention of this archetype is to generate a sample application that indicates how the application will look like after the steps below have been performed. It would be good to take a look at this project also to understand the structure of the project and how the implementations should look like.
Providing Metadata
The first step in the implementation should be to provide metadata for the service. To do this, you need an EDMX file representing the metadata for the OData V4 service. You can copy the EDMX file from the sample project under src/main/resources/edmx. In case you would like to create the EDMX file yourself, you can do so and save the EDMX file to the folder src/main/resources/edmx. The name of the file is considered as the service name. You should use the same name in a later step when you execute the service. You can rename the file to SampleService.xml for you to make use of the code snippets in this tutorial.
Implementation of Service
For providing implementations, you only need to follow the main steps as described below.
- Create a simple Java Class under the package you created when you created the project.
- Identify the operation you want to implement. In case you want to implement query for a Collection of an EntitySet called “Products” in your EDMX, you have to define a method as below
Use any name for the method, but ensure that the below conditions are met
Annotation Used | com.sap.cloud.sdk.service.prov.api.operations.Query |
Response Type | com.sap.cloud.sdk.service.prov.api.response.QueryResponse |
Request Type | com.sap.cloud.sdk.service.prov.api.request.QueryRequest |
- Populate the QueryResponse object with the data for the EntitySet. A simple implementation of populating the data(hard-coded sample data) using Map is shown below.
private List<Map<String, Object>> getSampleProducts() { List<Map<String, Object>> products = new ArrayList<Map<String, Object>>(); Map<String, Object> productEntity1 = new HashMap<String, Object>(); productEntity1.put("ProductID", 1); productEntity1.put("Name", "Laptop"); productEntity1.put("Description", "Professional Laptop"); productEntity1.put("Category", "Computers"); products.add(productEntity1); Map<String, Object> productEntity2 = new HashMap<String, Object>(); productEntity2.put("ProductID", 2); productEntity2.put("Name", "Monitor"); productEntity2.put("Description", "24-inch Desktop Monitor"); productEntity1.put("Category", "Display Monitors"); products.add(productEntity2); return products; } @Query(serviceName = "SampleService", entity = "Products") public QueryResponse getAllProducts(QueryRequest queryRequest) { List<Map<String, Object>> sampleData = this.getSampleProducts(); return QueryResponse.setSuccess() //indicates that the operation succeeded .setData(sampleData)//set the response data .response(); // returns the response }
The same approach could be used for other operations as described in the below table. You can easily identify the annotations to be used, the request type and response types by inspecting the packages below.
Annotations com.sap.cloud.sdk.service.prov.api.operations Request Types com.sap.cloud.sdk.service.prov.api.request Response Types com.sap.cloud.sdk.service.prov.api.response As an example, if you want to implement Create operation, you will use the Annotation com.sap.cloud.sdk.service.prov.api.operations.Create, a request of type com.sap.cloud.sdk.service.prov.api.request.CreateRequest and a response of type com.sap.cloud.sdk.service.prov.api.response.CreateResponse.
You can use the same class created in Step 1 above for the other operations or you could create new classes under the same package and things would just work fine.
The request body for Create can be obtained from the CreateRequest.getMapData() as a Map.
Build Project
Build the project by running the maven command(mvn clean install). On Eclipse, you can do this by selecting the pom.xml of the generated project and selecting Run As à Maven Install from the context menu.
In case you have not maintained the annotations properly or if the response types or request types are incorrect, this step will show those errors to you and your build will not succeed.
To deploy the project, you need to connect to your SAP Cloud Platform account.
Setup SAP Cloud Platform Account
To set up your SAP Cloud Platform account, please refer to the details here:
Ensure you have:
- Space to deploy your application
- Configured Cloud Foundry CLI
Logged on to the Cloud Foundry instance using the Console Client
Deploy Application
On the console client, change the directory to the root directory of the project you created from Eclipse. On the client, execute the command cf push. This will deploy the application to your Cloud Foundry space that you had logged in to.
Once the application is started, you will get the details of the application on the console client. You can get this information also by executing the command cf apps.
To execute the service, you need to append “/odata/V4/<ServiceName> to the URL you see on the console client. The final url will be https://<host>:<port>/odata/v4/SampleService. Your service is ready to be tested.
In addition to normal query, feel free to test operations like:
- $top
- $count
- $select
The CXSDK enables the above operations automatically without you having to implement any operation.
Hi Arun,
Nice blog on the service development SDK. Will this approach work if the underlying source is a REST service.
The scenario is that, I need to have a service that reads the response from a REST service , massages the data (optional), then exposes the same response as OData. What is the best way to achieve this? Is there any automatic framework that can be used?
Thanks
Srivatsan
Hi Srivatsan,
there is no automatic solution for this. You will have to create an OData model manually and bind the responses from your REST API calls. You can do this either with
Regards,
Jan
Hello Arun,
This is a great blog.
I have one question: where can I find javadoc regarding to SAP Cloud Platform SDK for Service Development?
Thanks in advance.
Tri
Hello Tri,
The javadocs are also available with the shipped jars in Maven Central. E.g. API Javadoc
Thanks,
Arun
Hi Arun,
This is nice article and very helpful. This will work for entity based end point exposure.
My requirement is that I have to provide an endpoint for validation separately rather than doing it as an function import on top of any UPDATE or CREATE. Could you provide the info if this is possible through Odata service approach.
Regards,
Kiran Kumar
Hi Kiran,
I think you are looking for a simple HTTP Endpoint that needs to be handled separately. You do not want to expose any entities for this validation right? If that is the case, you could extend the web application yourself to do this. You do not need the OData service approach.
Did I get your question right?
Thanks,
Arun
Hi Arun,
I followed the steps to create the service, and CF push was success, also I can see the application is started in cloud foundry space. But when I go to open the service definition by by url https://xxxxxxx.cfapps.sap.hana.ondemand.com/odata/v4/EPMSampleService/, it shows:This site can’t be reached.
I think the 'EPMSampleService' is the XML file name which is correct as I created. could you give me some hint for the error I encountered ?
I have solved it myself, I used proxy in the chrome, so destination can not be reached.
Hi Arun,
I followed the steps mentioned above and created a sample application. When i try to push the app to Cloud Foundry, i get the below error :
Could you please help in sorting this out ?
Kind Regards,
Vijay J