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
- Download Dynamic Response Handler and copy it to /usr/share/apache-tomcat-7.0.26/webapps/axis2/WEB-INF/modules
- \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
- 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
- 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 ConventionBelow is the example on how xml can be converted to JSON string for both the conventions which taken from Apache websiteXML 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/
Hello Niraj,
ReplyDeleteglad to see your post!
I followed your steps and I think I am almost there. However I don't manage to specify a namespace using badgerfish. I always get a "[ERROR] forceExpand: expected element namespace name, found
[ERROR] Element namespace from data source is http://www.examples.com/wsdl/HelloService.wsdl, not the expected..." error.
When I send it without a namespace it does not complain but the resulting soap element is missing the namespace (as expected) and fails because of that.
Do you have any idea how to fix that last bit?
Many thanks!
Roman
Great article Niraj.
ReplyDeleteThe only thing I missed was to add:
to the axis2.xml file.