[Download] | [Documentation Home] | [Release Note]

Resources Sample

Introduction

WSO2 data services supports making data available as a set of REST style resources. This sample will be demonstrating how data can be exposed as REST resources through a data service and also how the service can be invoked in REST style.

Prerequisites

The sample data service "ResourcesSample" should be deployed as per the instructions mentioned in Deploying Samples section.

Database Definition

The scenario is based on a possible database used in a small company to handle information about the products that are produced in the company.

Service Definition

There are seperate queries/resources written for specific tasks of the products; resources are defined inorder to access data through REST calls. The resources implemented in the service are listed as follows.

  • 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 infomation.
    Resource Path : product
    Resource Method : PUT

  • GET Product - Use HTTP GET request to retreive product infomation 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

Running the Sample

The service can be invoked in REST style via curl. You can execute the following commands in command line to invoke each of the resources.

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

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

  • GET Product
    curl -X GET http://localhost:9763/services/samples/ResourcesSample.HTTPEndpoint/product/S10_100

  • Get All Products
    curl -X GET http://localhost:9763/services/samples/ResourcesSample.HTTPEndpoint/products

  • Delete Product
    curl -X DELETE http://localhost:9763/services/samples/ResourcesSample.HTTPEndpoint/product/S10_100