This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.
Exposing Data as a REST Resource
The ESB profile of WSO2 Enterprise Integrator (WSO2 EI) allows you to expose data stored in various datasources as REST-style resources in addition to SOAP services. The tutorials on creating data services from scratch will guide you on how to expose data as SOAP services.
The following instructions explain how you can expose data as a REST resource:
Defining a REST resource
Let's take a data service that is already created using an RDBMS and expose the data as REST-style resources.
- Follow the tutorial on exposing an RDBMS as a data service to create the RDBMS data service.
- Log in to the product's Management Console.
- Click List under Main → Services. The RDBMS data service you created in the previous step will be listed.
- Click the data service to open the Service Dashboard of that data service.
- Click Edit Data Service (Wizard) to open the data service using the Create Data Service wizard.
- Click Next, until you get to the Resources screen.
- Click Add New Resource to open the Resources screen shown below. You can now expose the data as REST resource.
The fields in the above screen are explained below.- Resource Path: The resource name that is appended to the end of the resource URI. There are two ways of giving the resource path:
- Use a query path with the following format: <resource_path_name>/{Input_Parameter}
- Use a query parameter by giving a name for the resource path. For example, enter Product, if you are querying for products.
- Resource Method: The HTTP operation (GET, POST, PUT or DELETE).
Query ID: The corresponding query for the resource invocation.
- Resource Path: The resource name that is appended to the end of the resource URI. There are two ways of giving the resource path:
- Now, let's create the first resource by specifying a query path for the resource path. Enter values for the resource as shown below.
The values used above are as follows:- Resource Path: Enter
Account/{AccountID}
as the resource path. Note that we are specifying a query path and not a query parameter. - Resource Method: Select GET as the resource method.
- Query ID: Select Q1 as the query.
- Resource Path: Enter
- Save the resource.
- Now, let's create a second resource by specifying a query parameter for the resource path. Enter values for the resource as shown below.
The values used above are as follows:- Resource Path: Enter
Account
as the resource path. Note that we are specifying a query parameter and not a query path. - Resource Method: Select GET as the resource method.
- Query ID: Select Q1 as the query.
- Resource Path: Enter
- Click Finish to complete the data servie creation process.
Invoking the REST resource
The service can be invoked in REST-style via curl (http://curl.haxx.se).
Shown below is the curl command to invoke the GET resource using a query path.
curl -X GET http://localhost:8280/services/RDBMS.HTTPEndpoint/Account/1
Shown below is the curl command to invoke the GET resource using a query parameter.
curl -X GET http://localhost:8280/services/RDBMS.HTTPEndpoint/Account?AccountID=1
Sending message payloads
If you are sending request payloads to a REST resource defined in the data service, the object name specified in the payload must be in the following format: "_<HTTP_METHOD><RESOURCE_PATH>
", where RESOURCE_PATH
represents the path value specified in the data service resource. However, if the RESOURCE_PATH
specified in the data service contains the "/" symbol, be sure to replace the "/" symbol with the underscore symbol ("_") in the payload object name. See the following examples.
If the RESOURCE_PATH
is "wso2employee", the payload object name should be as follows:
- For HTTP POST requests: _postwso2employee
- For HTTP PUT requests: _putwso2employee
If the RESOURCE_PATH
is "wso2/employee", the payload object name should be as follows:
- For HTTP POST requests: _postwso2_employee
- For HTTP PUT requests: _putwso2_employee
If you are sending a batch request, and if the resource path is "wso2/employee", the payload object should be as follows:
For HTTP POST requests:
{ "_postwso2_employee_batch_req": { "_postwso2_employee": [ ......... ] } }
For HTTP PUT requests:
{ "_putwso2_employee_batch_req": { "_putwso2_employee": [ ......... ] } }