Wednesday, February 17, 2016

How to create OData Services with WSO2 DSS

whats covered: creating and consuming an odata conforming service with WSO2 DSS 3.5.0.

Liberating the data layer with services is an important step in converting a monolithic system to one that conforms to Service Oriented Architecture(SOA). WSO2 Data Services Server[1] is a solution targeted at this very requirement. In the past exposing a data-source(such as a RDBMS) as a service involved configuring the data source and mapping service operations(SOAP) or resources to predefined SQL queries(time consuming). With DSS version 3.5.0 onwards you can use Odata to provide more flexibility to data access and reduce the need of specialized SQL queries/operations.

Open Data is a protocol that specifies how data can be exposed conforming to RESTful architecture and how it can be queried using HTTP calls.


The anatomy of an Odata service URL



extracted from the Odata Spec[2]

 

Creating The Service



1) Create a Carbon Data-source


 
Navigate to Configure > Datasources > New Datasource and fill in the RDBMS details appropriately to create a data-source[3]. 

 

2) Generate a service and enable Odata


Generate a service[4] using the data-source created in step 1 . Edit the generated service, enable Odata from the datasource configuration page and publish the service. The Entity Model will be generated and the odata service will be available at,


https://<host>:<https port>/odata/<service name>




find the sql scripts and data service file used in this example here[5]

Consuming the service


The service is in conformance with the ODATA Protocol(version 4). You can access entities [6] and use query options[7] to filter and query the data as needed according to the protocol spec.


Example Invocations using CURL[8]



Select all data items belonging to BOOK resource.


curl -X GET -H 'Accept: application/json' 'https://localhost:9443/odata/RestaurantOfTheMindService/default/BOOK' -k

Select a single data item belonging to BOOK resource by PK,


curl -X GET -H 'Accept: application/json' 'https://localhost:9443/odata/RestaurantOfTheMindService/default/BOOK(1)' -k

Remove a data item belonging to BOOK resource by PK,


curl -X DELETE -H 'Accept: application/json' 'https://localhost:9443/odata/RestaurantOfTheMindService/default/BOOK(1)' -k


Select all data items belonging to BOOK resource where TITLE equals Cook.


curl -X GET -H 'Accept: application/json' 'https://localhost:9443/odata/RestaurantOfTheMindService/default/BOOK?$filter=TITLE%20eq%20%27Cook%27' -k


Select all data items belonging to BOOK resource where TITLE contains Coo.


curl -X GET -H 'Accept: application/json' 'https://localhost:9443/odata/RestaurantOfTheMindService/default/BOOK?$filter=contains(TITLE%2C%27Coo%27)' -k

Select all data items belonging to BOOK resource where TITLE contains Coo, result limited to 9 and ordered by TITLE

curl -X GET -H 'Accept: application/json' 'https://localhost:9443/odata/RestaurantOfTheMindService/default/BOOK?$filter=contains(TITLE%2C%27Coo%27)&$orderby=TITLE&$top=9' -k


note that the HTTP requests must be appropriately URL encoded.



No comments:

Post a Comment