Let’s try a simple scenario where a patient makes an inquiry specifying the doctor's specialization (category) to retrieve a list of doctors that match the specialization. The required information is stored in an H2 database that is shipped with this product. We will create a data service in the ESB profile of WSO2 Enterprise Integrator (WSO2 EI), which will expose the information in the database, thereby decoupling the client and the database layer in the back end. The client will then communicate with the data service hosted in WSO2 EI to get the required information instead of communicating directly with the back end.
...
Start the ESB profile:
Panel borderColor #542989 bgColor #ffffff borderWidth 1 Localtabgroup Localtab active true title On MacOS/Linux/CentOS Open a terminal and execute the following command:
Code Block wso2ei-6.4.0-integrator
Localtab title On Windows Go to Start Menu -> Programs -> WSO2 -> Enterprise Integrator 6.4.0 Integrator. This will open a terminal and start the ESB profile.
...
On the Main tab, click Create under Data Service.
In the Create Data Service screen, enter
DOCTORS_DataService
as the data service name, and click Next.Click Add New Datasource and enter the following details:
Field Value DatasourceID default
Datasourcetype Select RDBMS and default. Database Engine H2
Driver Class org.h2.Driver
URL jdbc:h2:file:./samples/data-services/database/DATA_SERV_QSG
User Name wso2ds
Password wso2ds
Click Save and then click Next to start defining a query.
Now, let's start writing a query to get data from the datasource.
First, you need to define a query to get the details of all the available doctors from the database.- Click Add New Query to open the Add New Query screen.
Enter the following values:
Field Value Query ID select_all_DOCTORS_query
Datasource default
SQL Code Block SELECT NAME, HOSPITAL, SPECIALITY, AVAILABILITY, CHARGE FROM PUBLIC.DOCTORS
Click Generate Response to automatically create mappings for the fields.
Under the Result (Output Mapping) section, change the values of the following fields.
Grouped by element DOCTORSCollection
Row name DOCTOR
The output mapping will be as shown below.
Info Output mapping specifies how the data that is fetched from your query will be shown in the response. Note that, by default, the output type is XML.
- Click Save.
- Now, let's create another query that can get the doctor information based on specialization.
- Click Add New Query to open the Add New Query screen.
Enter the following values:
Field Value Query ID select_DOCTORS_from_SPECIALITY_query
Datasource default
SQL Code Block SELECT NAME, HOSPITAL, SPECIALITY, AVAILABILITY, CHARGE FROM PUBLIC.DOCTORS WHERE SPECIALITY=?
Click Generate Input Mappings and a new input mapping record will be created.
Edit the record and change the Mapping Name to SPECIALITY, and click Save. You will now have the following input mapping:
Info Input mappings allow you to add parameters to a query so that you can set the parameter value when executing the query. According to the above definition, you need to provide the SPECIALTY SPECIALITY as an input in order to retrieve the data corresponding to the SPECIALTYSPECIALITY.
Click Main Configuration, to go back to the main configuration after adding the input mapping.
Click Generate Response to automatically create mappings for the fields.
Under the Result (Output Mapping) section, change the values of the following fields
Grouped by element DOCTORList
Row name DOCTOR
The output mapping will be as shown below.
Info Output mapping specifies how the data that is fetched from your query is shown in the response. Note that, by default, the output type is XML.
- Click Save and then click Next to open the Operations screen.
Since we are exposing the data as a REST resource, we do not need an operation. An operation is needed only if you are exposing the data as a SOAP operation. - Click Next to open the Resources screen.
Click Add New Resources to open the Add Resources screen.
Let's first create a resource to invoke theselect_all_DOCTORS_query
:Field Value Resource Path /getAllDoctors
Resource Method GET
Query ID select_all_DOCTORS_query
- Click Save to save the resource.
- Now, let's create another resource to invoke the
select_DOCTORS_from_SPECIALITY_query
.Click Add New Resources to open the Add Resources screen and enter the following details:
Field Value Resource Path /getDoctors
Resource Method GET
Query ID select_DOCTORS_from_SPECIALITY_query
- Click Save to save the resource.
- Click Finish after you have defined the resources to complete the data service creation process. You are now taken to the Deployed Services screen, which shows all the data services deployed on the server including the one you created.
...