axis2接收json_Eclipse+axis2+tomcat+json的搭建

package com.test.ws;

public class TestWebService {

public String getInfo(String str) {

return "Hello world " + str;

}

}

4.选中类名,点击右键-new一个web service,如图:点击next

5.引入axis2的jar包,如图:一路点击next。这是在将类转化为webservice

运行tomcat,访问地址http://localhost:8080/webservicetest/

可以看到如下画面:

这表明webservice发布成功,点击页面的service,会进入页面:

点击方法名TestWebService,地址是

http://localhost:8080/webservicetest/services/TestWebService?wsdl

就会出现:

6.建立webservice客户端webserviceclient,步骤如同建立webservice。

7.选中项目名称点击右键建立一个webservice client,如图:一路next

8.在生成的类中就会看到

/**

* Default Constructor

*/

public TestWebServiceStub(org.apache.axis2.context.ConfigurationContext configurationContext) throws org.apache.axis2.AxisFault {

this(configurationContext,"http://localhost:8080/webservicetest/services/TestWebService.TestWebServiceHttpEndpoint/" );

}

/**

* Default Constructor

*/

public TestWebServiceStub() throws org.apache.axis2.AxisFault {

this("http://localhost:8080/webservicetest/services/TestWebService.TestWebServiceHttpEndpoint/" );

}

这个地址是有用的,在测试写得方法的时候需要用到的

9.测试类

在控制台上打印:

public class Test {

public static void main(String[] args) {

try {

String url = "http://localhost:8080/webservicetest/services/TestWebService.TestWebServiceHttpEndpoint/";

TestWebServiceStub stub = new TestWebServiceStub(url);

GetInfo getInfo = new GetInfo();

getInfo.setStr("12");

stub.getInfo(getInfo);

} catch (AxisFault e) {

e.printStackTrace();

} catch (RemoteException e) {

e.printStackTrace();

}

}

}

利用url地址访问:

http://localhost:8080/webservicetest/services/TestWS/getInfo?str=tom

http://localhost:8080/webservicetest(工程名)/services/TestWS(类名)/getInfo(方法名)?str(传递的参数名,要和方法中传递的参数名一致)=tom(传递的参数值)

返回的是-

Hello world tom

如果想返回的数据是json格式的话,就需要配置一下axis2.xml文件

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 就是下载wso2dynamic-response.mar

3.  PatchJettison or download my patched version and replaceit with the one installed in WEB-INF/lib 就是下载jettison-1.2-patched.jar

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

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

6.  Add JSON Message builders to theaxis2.xml:

7.  Start your servlet container and testthe standard version service by calling this url:

http://localhost:8080/webservicetest/services/TestWS/getInfo?str=tom&response=application/json

在页面上显示的就是:

{"return":"Hello world tom"}

这个博客中讲解的挺详细的:

http://blog.163.com/eugeneheen_chen@126/blog/static/12081215720100204547231/

e1162e7dd16122ee73bfe3f509543a3d.png

大小: 56.7 KB

1daa769662b2d49b4b40299fcb319bb7.png

大小: 117.7 KB

485e61446d26fbd60bd149b955c68364.png

大小: 32.1 KB

883161f35addf5ca9cdeb411f246d3c2.png

大小: 101.8 KB

44e1141cfd2aa1eccdabbf9bd6a9fd66.png

大小: 106.9 KB

b14586d9b4563825b2d4e790dc4a9449.png

大小: 112.6 KB

d8aca015b0c3b09861b395147240e369.png

大小: 143.3 KB

92d623ab84be01cd2cd3399a9196f5b9.png

大小: 101.4 KB

c67c38b43f076df84f29b4bb25c62faa.png

大小: 2.8 KB

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2012-06-01 16:17

浏览 5726

评论

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值