Using Axis2 services from Javascript (by removing the XML namespaces)

If you want to call an Axis2 service from Javascript you will face the problem that the XML response of an Axis2 service call contains XML namespaces – something Javascript doesn’t like in cross-browser-friendly way.
The idea to fix this issue is to make an XSLT transformation directly from Axis2 that removes the unnecessary namespaces.

First we need an XSLT transformation that will do the job:

Then we’ll need a hand-made Axis2 conform MessageFormatter that uses the standard formatter for XML and performs the XSLT transformation on the result:

(Don’t forget to adjust the path of removeNamespaces.xsl, so the formatter can find the .xsl!)

Finally just put the class of the newly created formatter in WEB-INF/classes of your Axis2 deployment and replace the reference in the axis2.xml configuration file.
For this just exchange the line:

with:

After a restart of your application server you should be able to use your Axis2 services from Javascript – Have fun!

Building a JSON web service with Java and Axis2

It is said that in Java usually the easiest things are the hardest. The following is a nice example as it took me some time to figure out how to do it.
I wanted to build a simple URL request based web service in Java that returns an JSON object. Yes, you can do that with a simple Servlet too, the advantage of using Axis2 is that you can also call your deployed services using SOAP without any configuration changes.

  1. Download Axis2 as WAR and install it in your servlet container
  2. Download the DynamicResponseHandler module and add it to Axis by copying it to WEB-INF/modules
  3. Patch Jettison or download my patched version and replace it with the one installed in WEB-INF/lib
  4. Add the DynamicResponseHandler module reference to the axis2.xml configuration (located in WEB-INF/conf):
  5. Add the JSON Message formatters to the axis2.xml:
  6. Add JSON Message builders to the axis2.xml:
  7. Start your servlet container and test the standard version service by calling this url:
    http://localhost:8080/axis2/services/Version/getVersion?response=application/json

Now you are ready to add your own web services. Here you can find an example how to deploy a simple POJO service. Have fun!

Update: Zeno (see comments) sent me patch for usage with Jettison 1.2 – otherwise he received a NullPointerException. I haven’t checked it, but I hope it helps you! Thanks Zeno!