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 Axis2 isthat you can also call your deployed services using SOAP without anyconfiguration changes.

1.  Download Axis2 as WAR and install it inyour servlet container

2.  Download the DynamicResponseHandler module andadd it to Axis by copying it to WEB-INF/modules

3.  PatchJettison or download my patched version and replaceit with the one installed in WEB-INF/lib

4.  Add the DynamicResponseHandler modulereference to the axis2.xml configuration (located in WEB-INF/conf):

<module ref="DynamicResponseHandler"/>

5.  Add the JSON Message formatters to theaxis2.xml:

<messageFormatter contentType="application/json" class="org.apache.axis2.json.JSONMessageFormatter"/>

<messageFormatter contentType="application/json/badgerfish" class="org.apache.axis2.json.JSONBadgerfishMessageFormatter"/>

6.  Add JSON Message builders to theaxis2.xml:

<messageBuilder contentType="application/json" class="org.apache.axis2.json.JSONOMBuilder"/>

<messageBuilder contentType="application/json/badgerfish" class="org.apache.axis2.json.JSONBadgerfishOMBuilder"/>

7.  Start your servlet container and testthe 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 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!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值