Wednesday, June 5, 2013

Return JSON response from WSO2 Data Services Server




WSO2 Data Services Server is  an enterprise-grade, lean, 100% open source, cloud-enabled, multi-tenant data service hosting and management platform. This post explains how we can configure the WSO2 DSS to return the response in JSON format.

During this post I will be using WSO2 Data Services Server 3.0.1, which is the latest release at the time of this post. If you haven't got it already, we can go to the Data Services Server homepage and use the download link there or you can use this link to download it directly.

I will not be explaining any of the basics about Data Services Server since all of the information can be found using the documentation.

I will be using the ResourcesSample that is shipped with the DSS distribution to demonstrate this functionality. This sample explains how data can be exposed as a REST resource using WSO2 Data Services Server. What I'm going to explain is how we can get the response of these services as a JSON String.

1. Change the axis2.xml and axis2_client.xml


We need to add the following parameter to the axis2.xml and axis2_client.xml in the DSS server.  These files are found in CARBON_HOME/repository/conf/axis2 directory.

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

First, lets change the axis2.xml. I have added this parameter to the parameters section in the axis2.xml. Note that this parameter is not there by default and we have to add that.


Next, we need to set the value of this parameter in axis2_client.xml to 'true'. Note that this parameter is there in the axis2_client.xml and we only need to change the value.




2. Deploy the samples


Use the instructions given in the samples guide to deploy the samples.

1. Stop the currently running Data Services Server instance if any. This is recommended in order to clean the current sample database and redeploy the services.
2. Remove the current sample database and existing sample data services.
# cd CARBON_HOME/samples
# ant clean
3. Deploy all samples.
# ant
4. Start the Data Services Server
# cd CARBON_HOME/bin
# wso2server.bat|sh

3. Run the sample 


Im going to use CURL to send a request to the DSS server. I will be using the same command that is described in the DSS documentation.

curl -X GET http://localhost:9763/services/samples/ResourcesSample.HTTPEndpoint/product/S10_1678

As as result, I get the following response from the DSS server.


Now let us try to get the same response in JSON format. In order to do that, I'm going change my request and add the following header to the request

"Accept:application/json"

Now my new request looks like the following.

curl -X GET -H "Accept:application/json" http://localhost:9763/services/samples/ResourcesSample.HTTPEndpoint/product/S10_1678 

Now you will observe that the same response is sent as a JSON string.



We can follow the same approach get a JSON response from the other invocations that is described in the DSS sample.

3 comments:

  1. Do you know how to set the JSON response as the default one?
    What happens to me is that Im doing a JSONP request to the server and the Accept header goes like */* and DSS is resolving it as XML. How can I make sure that if the header goes like */* I get JSON as the response instead of XML?

    Please help me out. Thank you

    I tried changing the MessageFormatters and MessageBuilders in both axis2.xml and axis2_client.xml but that doesn't seem to work.

    < ! - - JSON Message Formatters - - >
    < messageFormatter contentType="*/*" class="org.apache.axis2.json.JSONMessageFormatter" / >
    < ! - - JSON Message Builders - - >
    < messageBuilder contentType="*/*"
    class="org.apache.axis2.json.JSONOMBuilder" / >

    ReplyDelete
  2. This curl command is not working in DSS 3.5

    curl -X GET -H "Accept:application/json" http://localhost:9763/services/samples/ResourcesSample.HTTPEndpoint/product/S10_1678

    Its giving below exception

    [2016-03-24 18:08:22,727] ERROR {org.wso2.carbon.dataservices.core.description.query.SQLQuery} - DS Fault Message: Error in XML generation at StaticOutputElement.execute_DS Code: UNKNOWN_ERROR_Nested Exception:-_javax.xml.stream.XMLStreamException: Invalid Staring element_ (Sanitized)
    DS Fault Message: Error in XML generation at StaticOutputElement.execute
    DS Code: UNKNOWN_ERROR
    Nested Exception:-
    javax.xml.stream.XMLStreamException: Invalid Staring element

    at org.wso2.carbon.dataservices.core.engine.StaticOutputElement.executeElement(StaticOutputElement.java:259)
    at org.wso2.carbon.dataservices.core.engine.OutputElement.execute(OutputElement.java:89)

    ReplyDelete
  3. HERE YOU HAVE GIVEN ABOUT Process server NJ IS VERY UNIQUE AND INFORMATIVE

    ReplyDelete