Thursday, October 6, 2011

How to send JSON messages to web services deployed on WSO2 Application Server

JSON (JavaScript Object Notation) is an open and text-based data exchange format, that provides a standardized data exchange format better suited for Ajax style web applications. You can find more information about JSON from www.json.org
WSO2 SOA middleware platform supports sending and receiving JSON messages. 
This post takes you through the steps to deploy a simple web service in WSO2 Application Serverand write a client using Axis2 ServiceClient API to invoke the service by sending JSON message.

Pre-requisites:
Download and install WSO2 Application Server 4.0.0 or later

Step 1
We are using a simple web service, which echo's user input as follows.

public class EchoService {
public OMElement echo(OMElement element) {
return element;
}
}

You can download the service archive (EchoService.aar) from here and deploy on WSO2 Application Server.

Step 2

Have a look at CARBON_HOME/repository/conf/axis2.xml (CARBON_HOME is the root directory of WSO2 Application Server). You will notice that the JSON specific message builders and formatters are enabled by default.

<!--JSON Message Formatters-->
<messageFormatter contentType="application/json"
class="org.apache.axis2.json.JSONMessageFormatter"/>
<messageFormatter contentType="application/json/badgerfish"
class="org.apache.axis2.json.JSONBadgerfishMessageFormatter"/>
<messageFormatter contentType="text/javascript"
class="org.apache.axis2.json.JSONMessageFormatter"/>

<!--JSON Message Builders-->
<messageBuilder contentType="application/json"
class="org.apache.axis2.json.JSONOMBuilder"/>
<messageBuilder contentType="application/json/badgerfish"
class="org.apache.axis2.json.JSONBadgerfishOMBuilder"/>
<messageBuilder contentType="text/javascript"
class="org.apache.axis2.json.JSONOMBuilder"/>

WSO2 Application Server accepts any JSON message with the content type application/json, application/json/badgerfish or text/javascript. In this example, we will use a message with the content-type, application/json

Step 3

Now, If you invoke the service using the Tryit utility associated with EchoService and trace the message, you will notice the payload of the SOAP message as follows.

<p:echo xmlns:p="http://service.carbon.wso2.org">
<echo xmlns="http://service.carbon.wso2.org" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">charitha</echo>
</p:echo>

Basically, the message payload will be similar to the following.

<echo>
<value>charitha</value>
</echo>

Step 4

Now, we are going to send the above payload as a JSON message. The JSON format of the above message payload will be as follows.

{"echo":{"value":"charitha"}}

Add the following class to your java project. Compile it using the libraries included in CARBON_HOME/repository/component/plugins directory. You may also need to add CARBON_HOME/lib/endorsed into your class path.

package org.wso2.carbon.service;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.ConfigurationContextFactory;
import java.io.ByteArrayInputStream;
public class JSONClient {
public static final String APPLICATION_JSON = "application/json";
public static void main(String[] args) {
String contentType = APPLICATION_JSON;
try {
EndpointReference targetEPR = new EndpointReference("http://localhost:9764/" +
"services/EchoService");
Options options = new Options();
options.setTo(targetEPR);
//set the required message Type when using JSON
options.setProperty(Constants.Configuration.MESSAGE_TYPE, contentType);
ConfigurationContext cc = ConfigurationContextFactory
.createConfigurationContextFromFileSystem(null,
"/home/charitha/products/wsas/client-repo/conf/axis2.xml");
ServiceClient sender = new ServiceClient(cc, null);
sender.setOptions(options);
options.setTo(targetEPR);
String payload = "charitha";
OMElement echoPayload = new StAXOMBuilder(new ByteArrayInputStream(payload.getBytes())).getDocumentElement();
OMElement result = sender.sendReceive(echoPayload);
System.out.println(result.getFirstElement().getText());
} catch (Exception e) {
e.printStackTrace();
}
}
}

You could use the same axis2.xml located at CARBON_HOME/repository/conf as the client axis2.xml when creating ConfigurationContext

If you forward the message to tcpmon and trace it, you will see the request as follows.

POST /services/EchoService HTTP/1.1
Content-Type: application/json; charset=UTF-8
User-Agent: WSO2 WSAS-3.2.0
Host: 127.0.0.1:9764
Transfer-Encoding: chunked

1d
{"echo":{"value":"charitha"}}
0

No comments:

Post a Comment