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/.

REST-Style Resources Sample

Note that WSO2 EI is shipped with the following changes to what is mentioned in this documentation:

  • <PRODUCT_HOME>/repository/samples/ directory that includes all Data Integration samples is changed to <EI_HOME>/samples/data-services/.
  • <PRODUCT_HOME>/repository/samples/resources/ directory that includes all artifacts related to the Data Integration samples is changed to <EI_HOME>/samples/data-services/resources/.

This sample demonstrates how data can be exposed as REST resources through a data service and also how the service can be invoked in REST style.

About the sample

This sample scenario is based on a database which stores information about the products of an enterprise. There are separate queries/resources written for specific tasks of the products. Resources are defined in order to access data through REST calls. The resources implemented in the service are listed below. 

  • Create Product - Use HTTP POST request to insert new products. 
    Resource Path : product 
    Resource Method : POST
  • Update Product - Use HTTP PUT request to update product information. 
    Resource Path : product 
    Resource Method : PUT
  • GET Product - Use HTTP GET request to retrieve product information for a given product code. 
    Resource Path : product/{productCode} 
    Resource Method : GET
  • Get All Products - Use HTTP GET request to get all the products. 
    Resource Path : products 
    Resource Method : GET

  • Delete Product - Use HTTP DELETE request to delete a given product from the database. 
    Resource Path : product/{productCode} 
    Resource Method : DELETE

See About Data Services and Resources for details.

Building the sample

The sample data service ResourcesSample should be deployed using the instructions in Samples Setup.

Running the sample

The service can be invoked in REST-style via curl (http://curl.haxx.se). The following commands can be executed using the command line to invoke each of the resources.

  • Create Product

    curl -X POST -d '<_postproduct><productCode>S10_111</productCode><productName>1969 Harley Davidson Ultimate Chopper</productName><productLine>Motorcycles</productLine><quantityInStock>7933</quantityInStock><buyPrice>48.81</buyPrice></_postproduct>' --header 'Content-Type: application/xml' http://localhost:8280/services/samples/ResourcesSample.HTTPEndpoint/product
  • Update Product

    curl -X PUT -d '<_putproduct><productCode>S10_100</productCode><productName>1969 Harley Davidson Ultimate Chopper</productName><productLine>Motorcycles</productLine><quantityInStock>8000</quantityInStock><buyPrice>48.81</buyPrice></_putproduct>' --header 'Content-Type: application/xml' http://localhost:8280/services/samples/ResourcesSample.HTTPEndpoint/product 
  • GET Product

    curl -X GET http://localhost:8280/services/samples/ResourcesSample.HTTPEndpoint/product/S10_100 
  • Get All Products

    curl -X GET http://localhost:8280/services/samples/ResourcesSample.HTTPEndpoint/products
  • Delete Product

    curl -X DELETE http://localhost:8280/services/samples/ResourcesSample.HTTPEndpoint/product/S10_100

Performing batch requests

Follow the steps below to perform batch requests for the above data service:

  1. Log in to the Management Console of WSO2 EI using the following URL: https://localhost:9443/carbon.

  2. Select the ResourcesSample data service that is listed in the Services → List menu.
  3. Click Edit Data Service (Wizard) to open the wizard and select the Enable Batch Request check box.
  4. Click Next and save the changes.

The HTTP request sent to the data service to create several products using cURL would be as follows:

curl -X POST -d '<_postproduct_batch_req><_postproduct><productCode>S10_172</productCode><productName>Suzuki</productName><productLine>Vans</productLine><quantityInStock>4222</quantityInStock><buyPrice>43.45</buyPrice></_postproduct><_postproduct><productCode>S10_173</productCode><productName>Honda</productName><productLine>Cars</productLine><quantityInStock>2564</quantityInStock><buyPrice>30.85</buyPrice></_postproduct></_postproduct_batch_req>' --header 'Content-Type: application/xml' http://localhost:8280/services/samples/ResourcesSample