Please refer Consume OData using SOA 11g post for SOA 11g version.
As part of this post, we will see how to consume JSON based OData Service using SOA 12c.
OData (Open Data Protocol), promoted by Microsoft, now an OASIS Standard, helps service providers to expose their data using RESTful APIs. OData supports, exposing of data in two Formats, Atom and JSON. Please refer OData WebSite for information about OData.
Public OData Services
One of the publicly available OData Service can be accessed at Public OData Service
Let us create a SOA 12c composite to consume the service for the following scenarios.
Following are the URI notations for
- List All Categories in JSON Format
- Filter and display Categories
- Display Category by Primary Key
Consume using REST Adapter
Let us create a SOA Application. Add a REST adapter to refer to Public ODATA Service.
- Add a Reference
- Configure Base URI as http://services.odata.org/V3/OData/OData.svc
- Configure Resource for Categories
- Add Method and query parameters
- Configured Reference Details
For the sake of simplicity, let us hard code values for OData Service Parameters like format and filter, inside the composite. Create BPEL Component (Synchronous, default values for rest of options).
Wire BPEL Component to OData Reference.
Inside BPEL, add variables for Input and Output
Add an invoke activity to ODATA Partner link.
Add an assign activity before the reply activity to copy from OData Service output to Output variable
Test Scenario 1 – List all categories in JSON Format.
Configure “Properties” in invoke to add value for “$format”. Providing value ‘json’ will make ODATA service to return data in JSON format. Otherwise it will return in Atom Format. Refer “URI Notations” section for the format of URIs.
Deploy and test the composite. As values are hard coded inside BPEL, give random string for composite input during testing. Composite should list all categories in JSON format.
Test Scenario 2 – Filter Categories with Name as Food.
Let us modify the composite to filter the results using $filter query parameter. Let us get Category with “Name” as Food”. “Name” is one of the property of “Category”. ODATA URI Notation for this scenario is
http://<base-uri>/Categories?$format=json&&$filter=Name eq 'Food'
Edit Invoke Activity to specify value for “$filter” query parameter
Deploy and test the composite. Result should show Category with Name “Food”.
Test Scenario 3 – Display Category using Primary Key
OData Protocol allows to list entities by primary key values using the notation
“ID” field is the primary key in “Categories” entity. To list Category refered by ID 1, URI notation is
To be able to query using this format , we need to build the URI dynamically and assign it to “rest.binding.requestURI” property of Invoke activity.
Already Configured “query” parameters will be ignored and only value of “rest.binding.requestURI” will be executed against REST Service. In our case, $format is needed. So, we will add it as part of the value that is assigned to “rest.binding.requestURI” property.
Deploy and test the composite. Category with ID=1 will be shown in the result.
With SOA 12c’s REST adapter we should be able to call OData Services successfully. If there is any use case for which REST adapter is not compatible, we can consider using Java Libraries for OData listed in Java Libraries for OData . Please refer this post which shows usage of OData4j library in SOA 11g environment.