SAP Cloud Platform SDK for service development: Create OData Service [1.1] easy, fast, unexplained
Don’t expect many words… just do it:
Create OData V4 service using
Java code
SAP Cloud Platform SDK for service development
SAP Cloud Platform
OK, enough polite words…;-)… now go ahead – and stay tuned for explanations in next blog(s) of this series
Project
See Prerequisites and Project Creation blog
Model
Create xml file with name DemoService.xml (see here) with the following content:
<?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="demo" xmlns="http://docs.oasis-open.org/odata/ns/edm">
<EntityType Name="Person">
<Key>
<PropertyRef Name="UniqueId" />
</Key>
<Property Name="UniqueId" Type="Edm.Int32" />
<Property Name="Name" Type="Edm.String" />
</EntityType>
<EntityContainer Name="container" >
<EntitySet Name="People" EntityType="demo.Person" />
</EntityContainer>
</Schema>
</edmx:DataServices>
</edmx:Edmx>
Code
Create a Java class with name ServiceImplementation.java in the generated package (!)
(see here) and add the following method:
@Read(serviceName="DemoService", entity="People")
public ReadResponse getPerson(ReadRequest request) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("UniqueId", 1);
map.put("Name", "Kitty");
return ReadResponse.setSuccess().setData(map).response();
}
Run
Build – Deploy – Run
https://<yourapp>.cfapps.sap.hana.ondemand.com/odata/v4/DemoService/Products(11)
End
Enough words
For (many) more words move to next blog or overview of blogs
Links
Overview of blog series and link collection
Appendix: Source code
Model file: DemoService.xml
<?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="demo" xmlns="http://docs.oasis-open.org/odata/ns/edm">
<EntityType Name="Person">
<Key>
<PropertyRef Name="UniqueId" />
</Key>
<Property Name="UniqueId" Type="Edm.Int32" />
<Property Name="Name" Type="Edm.String" />
</EntityType>
<EntityContainer Name="container" >
<EntitySet Name="People" EntityType="demo.Person" />
</EntityContainer>
</Schema>
</edmx:DataServices>
</edmx:Edmx>
Java Class: ServiceImplementation.java
package com.example.odata.DemoService;
import java.util.HashMap;
import java.util.Map;
import com.sap.cloud.sdk.service.prov.api.operations.Read;
import com.sap.cloud.sdk.service.prov.api.request.ReadRequest;
import com.sap.cloud.sdk.service.prov.api.response.ReadResponse;
public class ServiceImplementation {
@Read(serviceName="DemoService", entity="People")
public ReadResponse getPerson(ReadRequest request) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("UniqueId", 1);
map.put("Name", "Kitty");
return ReadResponse.setSuccess().setData(map).response();
}
}
Hi Carlos,
is it also possible to create Odata V2 services using Cloud SDK?
Greetings,
Ruchi
Hi Ruchi,
yes, there's an archetype for v2 available, rest is all the same
Cheers,
Carlos