REST in JSON with WSO2 Data Services

Being able to use JSON as the media type with RESTfull services with ease against your existing databases is one of the key advantages of the use of WSO2 Data services Server.

In a previous post, I showed you how to quickly generate MySQL data services. In there, the generated service has CRUD style operations with a SOAP style Web service.

Converting these generated services into REST style services is quite easy and quick.

First, you need to enable the WSO2 DSS instance to content negotiate, so that it can deal with JSON. To do this, add the following to the axis2.xml configuration. (This file is located inside wso2dss-3.1.x\repository\conf\axis2. )

<parameter name="httpContentNegotiation">true</parameter>

This is a one off server level setting, and for this to take effect, you need to restart the server, if it is already running. (Note: in the future released, this setting will come set to true by default)

Next, go to the management console (assuming you have the server running) and go to the generated service (I will use the same sample service as was used in MySQL data services blog in here).

Go to “Main” tab on the left menu and click on List, and click on the service name (in this sample CofeeShopService)

Home > Manage > Services > List > Service Dashboard

On the service dashboard page, go to bottom of the left side column and click on “Edit Data Service (XML Edit)”.

In the services description, we will do a bit of REST style design to get drinks.

  • We will use GET to access information about a given drink
  • Use the ID of the drink as the parameter identify the drink
  • Use the query “select_with_key_drink_query” to retrieve drink data from the database

The syntax to define the REST aspects with above points for an entity in the database is defined in the resource configuration documentation.

In accordance with these rules, our drink resource definition will look like:

<resource method="GET" path="drink/{id}">
     <call-query href="select_with_key_drink_query">
        <with-param name="id" query-param="id"/>
     </call-query>
  </resource>

You can add this definition at the end of the service descriptor in the service description XML editor. And save the service.

That is all, and you are ready to invoke the service. The URL pattern to use is:

http://localhost:9763/services/CoffeeShopService/drink/{id}

Now you can invoke this service using CURL command line tool:

curl -X GET -H "Accept: application/json" http://10.100.1.197:9763/services/CoffeeShopService/drink/2

In this invocation, we are using

  • HTTP GET method (using –X parameter)
  • application/json as the accepted content type (using –H parameter)
  • and we are accessing the drink with the ID 2 in this example

And you will get a response similar to:

{"drinkCollection":{"drink":{"id":"2","name":"Latte","cost":"4.50"}}}

There you go, simple & quick you have a REST service that is capable of dealing with JSON.

 

Technorati Tags:

Comments