Receiving JSON Responses
The data services mechanism is used to expose enterprise data as web services or as REST resources. Given below is an example of how you can configure AS to allow messages to be received in JSON format instead of XML.
The default setting is to use XML as the data format for sending and receiving messages. If you want to change this to another format, the system has to be manually configured by enabling 'content negotiation'.
To receive JSON responses, the 'content negotiation' property should be enabled in the axis2.xml and axis2_client.xml files which are stored in the <AS_Home/repository/conf directory. Note that this setting (JSON content negotiation) cannot be enabled as the default setting. After content negotiation is enabled, you must send requests to the server by adding “Accept:Application/json” to the request header, and as a result, you will receive the response in JSON format.
To change the axis2.xml file
- Open the axis2.xml file stored in <AS_Home>/repository/conf/axis2 directory.
Add <parameter name=“httpContentNegotiation”>true</parameter> to the list of parameters. Note that this parameter is not available by default in the axis2.xml file and requires to be added manually.
<axisconfig name="AxisJava2.0"> <!-- ================================================= --> <!-- Globally engaged modules --> <!-- ================================================= --> <module ref="addressing"/> <!-- ================================================= --> <!-- Parameters --> <!-- ================================================= --> <parameter name="hotdeployment">true</parameter> <parameter name="hotupdate">true</parameter> <parameter name="enableMTOM" locked="false">optional</parameter> <parameter name="cacheAttachments">true</parameter> <parameter name="attachmentDIR">work/mtom</parameter> <parameter name="sizeThreshold">4000</parameter> <parameter name=“httpContentNegotiation”>true</parameter> <parameter name=“EnableChildFirstClassLoading”>${childfirstCL}</parameter>
To change the axis2_client.xml file
- Open the axis2_client.xml file stored in <AS_Home>/repository/conf/axis2 directory.
Set the 'content negotiation' parameter to True. Note that this parameter is already available in the axis2_client.xml and it is only required to be enabled.
<axisconfig name="AxisJava2.0"> <!-- ================================================= --> <!-- Parameters --> <!-- ================================================= --> <parameter name="hotdeployment">true</parameter> <parameter name="hotupdate">false</parameter> <parameter name="enableMTOM">false</parameter> <!-- commons-http-client defaultMaxConnPerHost --> <parameter name="defaultMaxConnPerHost">500</parameter> <!-- commons-http-client maxTotalConnections --> <parameter name="maxTotalConnections">15000</parameter> <!--If turned on with use the Accept header of the request to determine the contentType of the response--> <parameter name=“httpContentNegotiation">True</parameter>
Test the JSON Configuration
After you have changed the axis2.xml file and axis2_client.xml files as explained above, use the ResourceSample, which is a sample available in your DSS distribution. This sample explains how data is exposed as a REST resource. Once this sample is deployed, send a request to the server using CURL by adding “Accept:Application/json” to the request header as shown below. Go to Exposing Data as REST-Style Resources for instructions on how to run this sample.
curl -X GET -H "Accept:application/json" http://localhost:9763/services/samples/ResourcesSample.HTTPEndpoint/product/S10_1678