Share this blog!

Sample data service using WSO2 Data Services Server

This post presents a sample data service created by WSO2's Data Services Server and incorporates a MySQL database.

Installing WSO2 DSS

You can follow the instructions on this document to install the server.

The database

I will be using MySQL as the source of the data. Therefore your machine must have MySQL installed in it. 

Quick tip: 

How to install MySQL in Ubuntu: use following commands.

$ sudo apt-get update
$ sudo apt-get install mysql-server
$ sudo mysql_secure_installation

To set up JDBC Driver: 

$ sudo apt-get install libmysql-java

and add ":/usr/share/java/mysql-connector-java.jar" to the PATH in etc/environment. Check related links for more information.


Additionally, you need to copy the mysql-connector-java-5.XX-bin.jar file into the /repository/components/lib/ directory.

I have created the following simple table(named person) in the database peopledb, which will be used in this sample.


Create the data service

Start the Data Services Server (In Linux, run command ./wso2server.sh in server_home/bin)


In the Main tab, under Services →Data Service, click Create. Specify a name and click Next.


Add new data source

Specify a name for the data source and set the parameters to point to the MySQL database.
Save and click Next.

Create a query

Just like its name stands for, the queries retrieve/write data. The following settings define a query to get all the entries of the table. This section introduces another query and an operation that uses a different sql query


The output is set as xml and People and Person are set as group and row so as to get an output in the form of:
<people>
    <person>
        ....info
    </person>
</people>

To map the output of the query to the xml tags, an output mapping must be defined. Click on Add New Output Mapping and set the mappings as follows.


Save and click Next.

Add operation to query

An operation can bind a query to the server. Simply define a name to the operation and set the query.
Click Finish and you will be redirected to the services page with your new service up and running.

Deploy the service

The new service will be running if the defined settings were correct. Clicking on the service will open the statistics of it.


As the Endpoints, it will show the URL of the service we just created. To use the operation we defined (getPeople) simply go the URL:-
http://172.17.0.1:9763/services/PersonDataService/getPeople

Multiple queries and operations

The following is another query added to the same data service to get information on one entry. Add a new query as follows:
I have used JSON as the output format. And bind an operation to it as follows. 

Here is another query - an insert query to add Barry to the table.

Related links



Next PostNewer Post Previous PostOlder Post Home

0 comments:

Post a Comment