This tutorial will guide you on how to expose a web resource as a data service using the ESB profile of WSO2 Enterprise Integrator (WSO2 EI). You can create a data service that can fetch selected data from a web resource and display the data in XML, JSON or RDF form.
Follow the instructions below. Also, see the samples in Data Integration Samples.
Creating a data service
Now, let's start creating the data service from scratch:
Download the product installer from here, and run the installer.
Let's call the installation location of your product the <EI_HOME> directory. This is located in a place specific to your OS as shown below:
OS Home directory Mac OS
Start the ESB profile:
- Log in to the management console of ESB profile of WSO2 EI using the following URL on your browser: "https://localhost:9443/carbon/".
- Click Create under the Data Service menu to open the Create Data Service window.
Enter the following data service name.
Data Service Name WebResource
- Leave the default values for the other fields.
- Click Next to go to the Datasources screen.
Connecting to the datasource
You can add a web resource as the datasource by following the steps given below.
Click Add New Datasource and enter the following details:
Datasource ID Enter WebHarvestDataSource. Datasource Type This specifies the type of datasource for which you will create the data service. Select Web Datasource from the list. Inline Web Harvest ConfigSelect Inline Web Harvest Config and enter the configuration as shown below:
- Save the datasource.
- Click Next to go to the Queries screen.
Creating a query to GET data
Now let's start writing a query for getting data from the web resource. The query will specify the data that should be fetched by this query, and the format that should be used to display the data.
Click Add New Query and enter the following details:
Query ID Enter webquery as the query ID. Datasource Select the datasource for which you are going to write a query. Select the web datasource that you created previously. Scrapper Variable Enter AppInfo. When you add a query to a Web datasource, you must enter a Scraper Variable. This scraper variable must be the same as the output name in the web datasource configuration, which returns the output from the configuration. In this example, the
var-defname in the configuration is
- Define Output Mapping: Now, let's specify how the data fetched from the datasource should be displayed in the output. We will create output mappings for the following data in the web resource: Title and Description.
Start by giving the following details:
Output Type Specify the format in which the query results should be presented. You can select XML, JSON or RDF. We will use XML for this tutorial. Grouped by element Specify a grouping for all the output mappings. This will be the XML element that will group the query result. Enter AppInfo in this field. Row Name Specify the XML element that should group each individual result. Enter App in this field.
- Click Add New Output Mapping to start creating the output mapping for the Title field.
- Click Add to save the output mapping.
Now, add another output mapping for the Description column.
You will now have the following output mappings listed for the webquery query:
- Click Next to go to the Operations screen.
Creating a SOAP operation to invoke the query
To invoke the query, you need to define an operation.
Click Add New Operation and enter the following information.
Operation Name GetResourceOp Query ID WebQuery
- Save the operation.
You can now invoke the data service query using SOAP.
Creating a REST resource to invoke the query
Now, let's create REST resources to invoke the query created above. Alternatively, you can create SOAP operations to invoke the queries. See the previous section for instructions.
Click Add New Resource and enter the following information.
Resource Path AppInfo Resource Method GET Query ID webquery
- Save the resource.
You can now invoke the data service query using REST.
Finish creating the data service
Once you have defined the operation, click Finish to complete the data service creation process. You will now be taken to the Deployed Services screen, which shows all the data services deployed on the server.
Invoking your data service using SOAP
You can try the data service you created by using the TryIt tool that is in your product by default.
- Go to the Deployed Services screen.
- Click the Try this service link for the WebResource data service. The TryIt Tool will open with the data service.
- Select the GetResource operation and click Send.
- The response will be published.
Invoking your data service using REST
You can send an HTTP GET request to invoke the data service using cURL as shown below.
This will return the response in XML.