Exposing Data as an OData Service
In this tutorial, we will run through the process of exposing and RDBMS as an OData service. When OData is enabled for a datasource, you do not need to manually define CRUD operations. They are automatically created.
Note that the OData feature can only be used for RDBMS, Cassandra, and MongoDB datasources.
Setting up an RDBMS
Follow the steps given below to set up a MySQL database for this tutorial.
- Download the product installer from here, and run the installer.
Let's call the installation location of your product the <EI_HOME> directory.If you installed the product using the installer, this is located in a place specific to your OS as shown below:
OS Home directory Mac OS /Library/WSO2/EnterpriseIntegrator/6.5.0
Windows C:\Program Files\WSO2\EnterpriseIntegrator\6.5.0\
Ubuntu /usr/lib/wso2/EnterpriseIntegrator/6.5.0
CentOS /usr/lib64/EnterpriseIntegrator/6.5.0
- Install the MySQL server.
- Download the JDBC driver for MySQL from here and copy it to your
<EI_HOME>/lib
directory. Create a MySQL database named
CompanyAccounts
.CREATE DATABASE CompanyAccounts;
Create a table in the
CompanyAccounts
database as follows.CREATE TABLE ACCOUNT(AccountID int NOT NULL,Branch varchar(255) NOT NULL, AccountNumber varchar(255),AccountType ENUM('CURRENT', 'SAVINGS') NOT NULL, Balance FLOAT,ModifiedDate DATE,PRIMARY KEY (AccountID));
Enter the following data into the table:
INSERT INTO ACCOUNT VALUES (1,"AOB","A00012","CURRENT",231221,'2014-12-02');
Expose the RDBMS as an OData service
Follow the steps given below.
Start the WSO2 ESB profile.
- Open the ESB profile's Management Console using
https://localhost:9443/carbon
, and log in usingadmin
as the username and the password. Click Create under Data Service.
Enter
ExposeMySQLDatabaseService
as the name of the data service, and click Next.Click Add New Datasource and enter details for the new datasource as follows.
Datasource ID Datasource Datasource Type RDBMS Datasource Type (Default/External) Leave Default selected. Database Engine MySQL Driver Class com.mysql.jdbc.Driver
URL jdbc:mysql://localhost:3306/Company
User Name Enter your MySQL server's username. Example: root Password Enter your MySQL server's password.
If you have not assigned a password, keep this field empty.Enable OData Select this check box. Click Finish to save the data service.
Go to the Deployed Services screen and click the data service that you created.
Access the data service using CRUD operations
Open a command prompt execute the following CURL commands using CRUD operations:
Note that you should have privileges to perform CRUD operations on the database. If not, the OData service will not work properly.
To get the service document:
curl -X GET -H 'Accept: application/json' http://localhost:8280/odata/ExposeDatabaseService/Datasource
To get the metadata of the service:
curl -X GET -H 'Accept: application/xml' http://localhost:8280/odata/ExposeDatabaseService/Datasource/$metadata
To read details from the ACCOUNT table:
curl -X GET -H 'Accept: application/xml' http://localhost:8280/odata/ExposeDatabaseService/Datasource/ACCOUNT