Eclipse+axis2+tomcat+json的搭建

刚开始接触webservice,对它不了解,但是慢慢学习吧,一点点滴来哈~
1.解压所下载的axis2-1.5.1-bin.zip,在Eclipse中的Windows-Preferences -web services-axis2 preferences-axis2 runtime目录下,选择刚解压缩的axis2的路径,然后ok

2.建立一个web project或者java project都可以。我这里建立的是web project名称为webservicetest。

3.在项目src下面建立包com.test.ws,然后建类TestWebService

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(传递的参数值)

返回的是

- <ns:getInfoResponse xmlns:ns="http://ws.com.cn">
<ns:return>Hello world tom</ns:return>
</ns:getInfoResponse>

如果想返回的数据是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):

<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/webservicetest/services/TestWS/getInfo?str=tom&response=application/json
在页面上显示的就是:

{"return":"Hello world tom"}

这个博客中讲解的挺详细的:
[url]http://blog.163.com/eugeneheen_chen@126/blog/static/12081215720100204547231/[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值