java Axis2 返回json格式
Januar 23rd, 2009 · 9 Comments
It is said that in Java usually theeasiest things are the hardest. The following is a nice example as it took mesome time to figure out how to do it.
I wanted to build a simple URL request based web service in Java that returnsan JSON object. Yes, you can do that with a simple Servlet too, the advantageof using
1. Download Axis2 as WAR and install it inyour servlet container
4. Add the DynamicResponseHandler modulereference to the axis2.xml configuration (located in WEB-INF/conf):
<moduleref="DynamicResponseHandler"/>
5. Add the JSON Message formatters to theaxis2.xml:
<messageFormattercontentType="application/json" class="org.apache.axis2.json.JSONMessageFormatter"/>
<messageFormattercontentType="application/json/badgerfish" class="org.apache.axis2.json.JSONBadgerfishMessageFormatter"/>
6. Add JSON Message builders to theaxis2.xml:
<messageBuildercontentType="application/json" class="org.apache.axis2.json.JSONOMBuilder"/>
<messageBuildercontentType="application/json/badgerfish" class="org.apache.axis2.json.JSONBadgerfishOMBuilder"/>
Now you are ready to add your own webservices. Here you can find an examplehowto 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 hopeit helps you! Thanks Zeno!