OData Sample
Exposing a database as an OData service eliminates the need to define queries, operation etc. to access the information in the database. OData (Open Data Protocol) is an OASIS standard that defines the best practice for building and consuming RESTful APIs. You can easily expose databases as an OData service using WSO2 Data Services Server.
About the sample
This OData sample was generated to work with the default h2 database (DATA_SERV_SAMP.h2.db
), which used for samples in the DSS product. This database is stored in the <DSS_HOME>/samples/database
directory. The details of this database are as follows:
Driver class: org.h2.Driver
JDBC URL: jdbc:h2:file:./samples/database/DATA_SERV_SAMP
Tables in the DB:
- CUSTOMERS
- ACCOUNTS
- PAYMENTS
- ORDERS
- PRODUCTLINES
- FILES
- PRODUCTS
- FILERECORDS
- EMPLOYEES
- OFFICES
- DEPARTMENT
- ORDERDETAILS
You can find the DDL of these sql tables in <DSS_HOME>/samples/sql/h2
folder.
Building the sample
The sample data service named ODataSampleService
should be deployed and the server should be started using the instructions in Samples Setup.
Executing the sample
You can easily invoke the service using HTTP requests. See the following examples.
To get the service document:
GET https://localhost:9443/odata/ODataSampleService/default Accept : application/json
To get the meta data of the service:
GET https://localhost:9443/odata/ODataSampleService/default/$metadata Accept : application/xml
To read customer details:
GET https://localhost:9443/odata/ODataSampleService/default/CUSTOMERS Accept : application/json
To add accounts details, send the HTTP request as shown below. In this example we will be creating an account ID named "1".
POST https://localhost:9443/odata/ODataSampleService/default/ACCOUNTS Accept: application/json Content-Type: application/json Prefer : return=representation { "BALANCE" : 12.22 ,"ACCOUNTID" : 1 }
To delete account details, send the HTTP request as shown below. In this example, we will be deleting the account ID "1".
DELETE https://localhost:9443/odata/ODataSampleService/default/ACCOUNTS(1) Accept: application/json
To update account details:
PUT https://localhost:9443/odata/ODataSampleService/default/ACCOUNTS(1) Accept: application/json { "BALANCE" : 12.22 }
To invoke a particular query, send the HTTP request as shown below. In this example, we will receive the city and phone numbers of customers.
GET https://localhost:9443/odata/ODataSampleService/default/CUSTOMERS?$select=CITY,PHONE Accept: application/json
To filter information based on a query, send the HTTP request as shown below. In this example, we will receive the employee details connected to the 'Nantes' city.
GET https://localhost:9443/odata/ODataSampleService/default/CUSTOMERS?$filter=CITY eq 'Nantes' Accept: application/json