Friday, September 5, 2014

Expose Data as a Service using WSO2 Data Services Server (DSS)

Scenario:
You run a Zoo... yeah that's right a Zoo. You have a set of animals and each of them are renowned for something. You have their information which include an id for each animal, their country of origin, the name you gave them and what they are known for.

You need to expose this animal information as a web service.

So what do you do.

  • You open up eclipse
  • Create a web services project
  • Add the db drivers
  • Create the DAO layer
  • Create the DTO ....
WHoA WHOaaa... STOP ... 

That's ancient science... in the time u did all that you could have had your services exposed and be already testing them. 

How ?

Well this amazing little product called the WSO2 Data Services Server (DSS for short). It'll make your life so much more easy when it comes to exposing your data as services.

In this example we'll look at how to expose data as a SOAP service in a few minutes with just a few clicks, without writing a single line of code.

Yep .. your heard me.. without a single line of code .. 

What you need:
  1. MySQL Database and JDBC driver (or any other db + driver of your choice)
  2. WSO2 DSS 

Step 1:

Create a database called CUSTOMER_DB
create database Zoo_db

Create table CUSTOMER

CREATE TABLE `animal` (
  `AnimalID` varchar(90) NOT NULL PRIMARY KEY,
  `Species` varchar(200) DEFAULT NULL,
  `Name` varchar(200) DEFAULT NULL,
  `Country` varchar(200) DEFAULT NULL,
  `Speciality` varchar(200) DEFAULT NULL
);

Insert the animal data:

insert into animal values ("001", "Penguin", "Skipper", "Madagaskar", "Leading a pack of Penguins");
insert into animal values ("002", "Penguin", "Kowalsky", "Madagaskar", "Research & Development");
insert into animal values ("003", "Penguin", "Rico", "Madagaskar", "Demolitions");
insert into animal values ("004", "Penguin", "Private", "Madagaskar", "hmmm.. saying random feminine stuff");
insert into animal values ("005", "Lemur", "Julian", "Madagaskar", "Dance Unit");
insert into animal values ("006", "Lemur", "Maurice", "Madagaskar", "Maintenance");
insert into animal values ("007", "Lemur", "Mort", "Madagaskar", "Feet Hugging");

Step 2:
  • Download and Unzip WSO2 DSS
  • Add the MySQL connector JAR to the <DSS_HOME>\repository\components\dropins directory
  • Start the server by running the wso2server.bat (Windows) or wso2server.sh (Linux) in <DSS_HOME>/bin directory
  • The management console will be accessible on https://localhost:9443/carbon/ - 9443 is the default port. If you want to change this you can do it by offsetting the port in <DSS_HOME>\repository\conf\carbon.xml - Increment the <Offset>0</Offset> value. All ports used by DSS will be incremented accordingly.
  • Login with default admin username/password admin:admin
  • Using the controls on the left go to Configure -> Data Sources -> Add New Data Source


  • Choose RDBMS, Give a name, Enter Driver String and URL, username/password as shown


Test and save the DataSource

Next go to Main, Under Services -> DataServices click Generate:




Select the created ZooDS from the dropdown and give the DB name we created (Zoo_db)
And then select the table for which you want the services generated for. In our case the animal DB



click next and you'll get the option to select whether to create separate services for the different operations (select, insert, delete etc.) or bundle them to one service. We'll select to expose all in one service and give it the name "ZooDataService"



Click next and then Finish. This will create a set of services that expose all the operations such as Select, Insert, Delete etc. as soap servicesNow your service will be listed under Services -> list

Now you can try out the different operations using the 'Try This Service' option which you'll see




And there you go ... Who said exposing data required hours of coding ?

So to sum it up WSO2 DSS is like Rico from Penguins of Madagaskar... you'll know what I mean if u've watched it... Tools at your fingertip.. Fast and Easy .. Just like that ;)

No comments:

Post a Comment