Tuesday, May 22, 2012

How to write & Configuring JSON web service for Apache Axis2


Recently I was working on one of the project where there was requirement of implementing the Axis2 Web service with JSON support. Here I have used Apache Tomcat 7.0 Web Server for configuration. During initial phase I have gone through the documentation provided on Apache site for JSON Support and thought it would be easy to implement as we can write any POJO service first and that SOAP Service can be exposed as JSON Web Service.However it was not so simple as there were issues in the library used ie. Jettison. This library requires patch to support functionality.

JSON stands for Java Script Object Notation. And it is data exchange format similar to XML. JSON is very light weight and becoming popular these days.It defines data in key value pair as we are doing in case of Properties file however here we maintain parent child relationship as well. You can find more information on this on JSON website.

As this article is written on how to configure JSON with Apache Axis2 on Tomcat 7.0. Below details cover only technical information on how to configure JSON and writing JSON Web Service.

For this article I am using Tomcat version 7.0.26 and it has been installed Red hat 5.6.Axis2 is installed and configure as explain in Apache's Axis2 Website. 

Tomcat Installation directory is : /usr/share/apache-tomcat-7.0.26/
Axis2 Installation directory : /usr/share/apache-tomcat-7.0.26/webapps


JSON Configuration changes on Tomcat 7.0

  1. Download Dynamic Response Handler and copy it to  /usr/share/apache-tomcat-7.0.26/webapps/axis2/WEB-INF/modules    
  2. \Go to /usr/share/apache-tomcat-7.0.26/webapps/axis2/WEB-INF/lib and rename jettison-1.0-RC2.jar Using below command         mv jettison-1.0-RC2.jar jettison-1.0-RC2.jar.bak  
  3.  Provide update jettison jar file. Copy patched jettison-1.2-patched.jar to /usr/share/apache-tomcat-7.0.26/webapps/axis2/WEB-INF/lib location & restart tomcat
  4. Modify axi2.xml at location /usr/share/apache-tomcat-7.0.26/webapps/axis2/WEB-INF/conf Add following Message Formatters and Message Builders.



    <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"/> -->
    <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"/>-->
    
    

    5. When we are performing conversion from XML to JSON and viceversa. There are two convention that we follow. which are mentioned above.
    - Mapped Convention
    - Badgerfish Convention

    Below is the example on how xml can be converted to JSON string for both the conventions which taken from Apache website

    XML If we use the namespace mapping as http://foo.com -> foo
    <xsl:root xmlns:xsl="http://foo.com"><data>my json string</data></xsl:root>


    Equivalent JSON (Mapped Convention)
    {"foo.root":{"data":"my json string"}}

    Equivalent JSON (Badgerfish Convention)
    {"xsl:root":{"@xmlns":{"xsl":"http://foo.com"},"data":{"$":"my json string"}}}


    Now you can simple develop your any web service application and can communicate with that service using JSON.

    6. You can simply create Hello World application to echo request message. and call that HelloWorld service as below get the response in JSON format.

    Mapped Convention : Http://localhost:8080/axis2/services/HelloWorldService/echoMethod/response=application/json

    Badgerfish Convention: Http://localhost:8080/axis2/services/HelloWorldService/echoMethod/response=application/json/
    7. And while sending a request to JSON service content-type should be set to application/json.

    I have not included the steps here to create POJO Service, this is very well documented on Apache website.

    Reference sites :
    http://jettison.codehaus.org/
    http://axis.apache.org/axis2/java/core/docs/json_support.html
    http://www.marcusschiesser.de/2009/01/building-a-json-web-service-with-java-and-axis2/