Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
arunbhaskaran_nair
Discoverer
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:

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.

  1. 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.

  2. 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.

  1. Create a simple Java Class under the package you created when you created the project.

  2. 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

 

  1. 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:

https://help.sap.com/viewer/65de2977205c403bbc107264b8eccf4b/Cloud/en-US/b8ee7894fe0b4df5b78f61dd1ac...

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.
9 Comments