This tutorial will guide you on how to expose a web resource as a data service using 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.
Start the Create New Data Service wizard
Now, let's start creating the data service from scratch:
Add a Web Resource as the datasource
You can add a web resource as the datasource by following the steps given below.
- Click Add New Datasource to open the following screen:
- Enter the following values:
- In the Datasource ID field, enter WebHarvestDataSource as the datasource Id.
- In the Datasource Type field, specifies the type of datasource for which you will create the data service. Select Web Datasource from the list. You will now get the following screen:
Select Inline Web Harvest Config and enter the configuration as shown below:
- Save the datasource.
- Click Next to go to the Queries screen.
Define a query for the datasource
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 to open the Add New Query screen.
- Enter the following values:
- Query ID: In this field, enter webquery as the query ID.
- Datasource: In this field, select the datasource for which you are going to write a query. Select the web datasource that you created previously.
- Scrapper Variable: 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.
- In the Output type field, 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.
- In the Grouped by element field, 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.
- In the Row Name field, 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. Enter values as shown below:
- 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:
Find out more about defining Output Mappings.
- Click Next to go to the Operations screen.
Defining and operation to invoke the query
Follow the steps given below.
- Click Add New Operation to open the Add New Operation screen.
- In the Operation Name field, enter GetResource.
- In the Query ID field, select WebQuery.
- Save the operation.
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
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.