Saturday, March 1, 2014

How to get a JSON response with WSO2 Data Services Server (wso2dss) ?

Latest version of DSS can be downloaded from here. (At the moment it's wso2dss-3.1.1).

Data Services Server support for both XML and JSON outputs. In order to get a JSON output, you need to change following configurations.

Add following parameter to 'axis2.xml' and in 'axis2_client.xml'. (In axis2_client.xml this parameter is already there, but its default value is set to false. These two files are located in '{DSS_HOME}/repository/conf/axis2/' directory).

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

Note that if you are using tenant, then the above parameter need to be set in 'tenant-axis2.xml' as well.

Restart the server. We are done.

We need to use content-negotiation when doing the requests. i.e We need to pass 'Accept' header with the request. Example request done for customersInBoston operation in the RDBMSSample is given bellow. (This sample 'RDBMSSample' service is deployed in server by default).

curl -v -H "Accept:application/json" http://localhost:9783/services/samples/RDBMSSample/customersInBoston

Response
{"customers":{"customer":[{"customer-name":"Gifts4AllAges.com","contact-last-name":"Yoshido","contact-first-name":"Juri","phone":"6175559555","city":"Boston","country":"USA"},{"customer-name":"Diecast Collectables","contact-last-name":"Franco","contact-first-name":"Valarie","phone":"6175552555","city":"Boston","country":"USA"}]}}

5 comments:

  1. Hi Dinusha:
    Thanks for nice blog and is very helpful. I have question. How to return custom response code and message in DSS when the select query or stored procedure returns null data.
    Example:
    SELECT custname, custadd from cust_table where custnum = : custnum

    Now this select query assume returns null(no data found for the customer number supplied )
    In that case, I want to return a JSON with error
    {
    "Error" :

    {
    "error_code" : 25,
    "error_msg" : "No customer found for :customer Number"
    "error_status" : "ERROR"
    }
    }

    Let me know if there is way to achieve with DSS.

    Thx
    Venkat

    ReplyDelete
  2. How do I modify the data in sample database using the above rest method ??

    I tried this but no result
    /services/samples/RDBMSSample/setEmployeeSalary/1165/5000

    ReplyDelete
  3. Working with GET only not with POST

    ReplyDelete
  4. This was helpful. Thanks for the post :)

    ReplyDelete
  5. Thanks for the online support all ..

    ReplyDelete

Note: Only a member of this blog may post a comment.