Axis2 C# Java Programming Technology Uncategorized Web Development

Building a JSON Axis2 REST Client for a WCF Service.

Using JSON as the message format for a WCF REST service has some advantages
if you are building a backend service for a purely browser based application.

For instance, JSON is natively supported in all of the major web browsers.

I had to build a REST client using Axis2 that uses the JSON message format.

The blog post Enabling JSON in Apache Axis2 explains the conventions that are used to map XML to JSON and how to configure axis2.xml so that your client application can use the XML/JSON mapping convention of your choice.

I chose the mapped namespace convention which can be configured by adding the two lines to your axis2.xml

<messageFormatter contentType="application/json"

<messageBuilder contentType="application/json"

If you choose the mapped namespace convention you may get an exception that says “JSON mapped namespaces are not supported in Axis2”. If you see the above Axis2 exception then you need to patch jettison.

Instructions on the exception and the patch are in the “Building a JSON web service with Java and Axis2” post by Marcus Schiesser.

The ServiceClient (org.apache.axis2.client.ServiceClient) object can be used to make REST API calls in Axis2.

The ServiceClient can be configured using the Options (org.apache.axis2.client.Options) object.

For a REST style service invocation you need the following options,

Options options = new Options();
options.setTo(new EndpointReference("http://your.service.endpoint.url"));
options.setProperty(Configuration.MESSAGE_TYPE, "application/json");
options.setProperty(Configuration.ENABLE_REST, Constants.VALUE_TRUE);

//reuse HTTP clients
options.setProperty(HTTPConstants.REUSE_HTTP_CLIENT, Constants.VALUE_TRUE);
options.setProperty(HTTPConstants.CACHED_HTTP_CLIENT, httpClient);

The httpClient object is created via the MultiThreadedHttpConnectionManager. This is needed for managing the HTTP connections if your client is used in a multithreaded application. See the post “Improving Axis2 Client HTTP Transport Performance”

Finally, you have to engage the addressing module and set the ServiceClient options

client = new ServiceClient();

The ServiceClient sendReceive call takes an OMElement which I created using custom XML serialization on the message objects.

Make sure you cleanup the http connections via the httpconnectionManager used to create the httpClient. Again. see the post “Improving Axis2 Client HTTP Transport Performance” on how to do this correctly.

The sendReceive call can be made non-blocking if you are looking for further improvements in performance.

The response returned by the sendReceive call is an OMElement again and can be de-serialized using custom XML de-serialization on your message objects.

If you are looking for pure SOAP interoperability between WCF .NET SOAP client and an Axis2 service be sure to chek out my other post Axis2 and WCF .Net Interoperability (Interop)

Leave a Reply

Your email address will not be published. Required fields are marked *