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.
- Download Axis2 as WAR and install it in your servlet container
- Download the DynamicResponseHandler module and add it to Axis by copying it to WEB-INF/modules
- Patch Jettison or download my patched version and replace it with the one installed in WEB-INF/lib
- Add the DynamicResponseHandler module reference to the axis2.xml configuration (located in WEB-INF/conf):
- Add the JSON Message formatters to the axis2.xml:
XHTML12<messageFormatter contentType="application/json" class="org.apache.axis2.json.JSONMessageFormatter"/><messageFormatter contentType="application/json/badgerfish" class="org.apache.axis2.json.JSONBadgerfishMessageFormatter"/>
- Add JSON Message builders to the axis2.xml:
XHTML12<messageBuilder contentType="application/json" class="org.apache.axis2.json.JSONOMBuilder"/><messageBuilder contentType="application/json/badgerfish" class="org.apache.axis2.json.JSONBadgerfishOMBuilder"/>
Start your servlet container and test the standard version service by calling this url:
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!