- 浏览: 46157 次
文章分类
最新评论
eclipse+webservice开发实例
1.参考文献:
1.利用Java编写简单的WebService实例 http://nopainnogain.iteye.com/blog/791525
2.Axis2与Eclipse整合开发Web Service http://tech.ddvip.com/2009-05/1242968642120461.html
3.http://blog.csdn.net/lightao220/article/details/3489015
4.http://clq9761.iteye.com/blog/976029
5.使用Eclipse+Axis2+Tomcat构建Web Services应用(实例讲解篇)
2.实例1(主要看到[2])
2.1.系统功能:
2.2.开发前准备:
- 安装Eclipse-jee;
- 下载最新版本的Axis2,网址http://axis.apache.org/axis2/java/core/download.cgi,选择Standard Binary Distribution的zip包,解压缩得到的目录名axis2-1.4.1,目录内的文件结构如下:
2.3.开发前配置:
在Eclipse的菜单栏中,Window --> Preferences --> Web Service --> Axis2Perferences,在Axis2 runtime location中选择Axis2解压缩包的位置,设置好后,点"OK"即行。(如图)
2.4.开发Web Service:
(1)新建一个Java Project,命名为"WebServiceTest1"
(2)新建一个class,命名为"CalculateService",完整代码如下:
- packageedu.sjtu.webservice;
- /**
- *计算器运算
- *@authorrongxinhua
- */
- publicclassCalculateService{
- //加法
- publicfloatplus(floatx,floaty){
- returnx+y;
- }
- //减法
- publicfloatminus(floatx,floaty){
- returnx-y;
- }
- //乘法
- publicfloatmultiply(floatx,floaty){
- returnx*y;
- }
- //除法
- publicfloatdivide(floatx,floaty){
- if(y!=0)
- {
- returnx/y;
- }
- else
- return-1;
- }
- }
- packageedu.sjtu.webservice;
- /**
- *计算器运算
- *@authorrongxinhua
- */
- publicclassCalculateService{
- //加法
- publicfloatplus(floatx,floaty){
- returnx+y;
- }
- //减法
- publicfloatminus(floatx,floaty){
- returnx-y;
- }
- //乘法
- publicfloatmultiply(floatx,floaty){
- returnx*y;
- }
- //除法
- publicfloatdivide(floatx,floaty){
- if(y!=0)
- {
- returnx/y;
- }
- else
- return-1;
- }
- }
(4)下一步(next),在出现的Web Services对象框,在Service implementation中点击"Browse",进入Browse Classes对象框,查找到我们刚才写的写的CalculateService类。(如下图)。点击"ok",则回到Web Service话框。
(5)在Web Service对话框中,将Web Service type中的滑块,调到"start service“的位置,将Clienttype中的滑块调到"Test client"的位置。
(6)在Web Service type滑块图的右边有个"Configuration",点击它下面的选项,进入Service Deployment Configuration对象框,在这里选择相应的Server(我这里用Tomcat6.0)和Web Service runtime(选择Apache Axis2),如下图:
(7)点OK后,则返回到Web Service对话框,同理,Client type中的滑块右边也有"Configuration",也要进行相应的置,步骤同上。完成后,Next --> next即行。进入到Axis2 Web Service Java Bean Configuration,我们选择Generate a default services.xml,如下图所示:
(8)到了Server startup对话框,有个按键"start server"(如下图),点击它,则可启动Tomcat服务器了。
(9)等启完后,点击"next -- > next",一切默认即行,最后,点击完成。最后,出现如下界面:(WebService Explorer),我们在这里便可测试我们的Web服务。(使用浏览器打开的话使用如下地址:http://127.0.0.1:19189/wse/wsexplorer/wsexplorer.jsp?org.eclipse.wst.ws.explorer=3)。如下图所示:
注:在浏览器中打开Web Service Explorer(有时候在eclipse中关闭了webservice explorer,可以用这种方法打开)
首先登录地址:http://127.0.0.1:19189/wse/wsexplorer/wsexplorer.jsp。然后在网页右上角选择Web Service Exoplorer标签。然后输入WSDL地址:http://localhost:8080/WebServiceTest1/services/CalculateService?wsdl 。这个wsdl地址就是我们刚才发布服务的那个wsdl。点击go,如下图所示:
然后就可以看到如下界面了:
(10)测试比较简单,例如,我们选择一个"plus"的Operation(必须是CalculateServiceSoap11Binding),出现下图,在x的输入框中输入1,在y的输入框中输入2,点击"go",便会在status栏中显示结果3.0。其他方法的测试也类似。结果如上图所示。
2.5.CalculateService客户端调用程序
- packageedu.sjtu.webservice.test;
- importjavax.xml.namespace.QName;
- importorg.apache.axis2.AxisFault;
- importorg.apache.axis2.addressing.EndpointReference;
- importorg.apache.axis2.client.Options;
- importorg.apache.axis2.rpc.client.RPCServiceClient;
- publicclassCalculateServiceTest{
- /**
- *@paramargs
- *@throwsAxisFault
- */
- publicstaticvoidmain(String[]args)throwsAxisFault{
- //TODOAuto-generatedmethodstub
- //使用RPC方式调用WebService
- RPCServiceClientserviceClient=newRPCServiceClient();
- Optionsoptions=serviceClient.getOptions();
- //指定调用WebService的URL
- EndpointReferencetargetEPR=newEndpointReference(
- "http://localhost:8080/WebServiceTest1/services/CalculateService");
- options.setTo(targetEPR);
- //指定要调用的计算机器中的方法及WSDL文件的命名空间:edu.sjtu.webservice。
- QNameopAddEntry=newQName("http://webservice.sjtu.edu","plus");//加法
- QNameopAddEntryminus=newQName("http://webservice.sjtu.edu","minus");//减法
- QNameopAddEntrymultiply=newQName("http://webservice.sjtu.edu","multiply");//乘法
- QNameopAddEntrydivide=newQName("http://webservice.sjtu.edu","divide");//除法
- //指定plus方法的参数值为两个,分别是加数和被加数
- Object[]opAddEntryArgs=newObject[]{1,2};
- //指定plus方法返回值的数据类型的Class对象
- Class[]classes=newClass[]{float.class};
- //调用plus方法并输出该方法的返回值
- System.out.println(serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs,classes)[0]);
- System.out.println(serviceClient.invokeBlocking(opAddEntryminus,opAddEntryArgs,classes)[0]);
- System.out.println(serviceClient.invokeBlocking(opAddEntrymultiply,opAddEntryArgs,classes)[0]);
- System.out.println(serviceClient.invokeBlocking(opAddEntrydivide,opAddEntryArgs,classes)[0]);
- }
- }
- packageedu.sjtu.webservice.test;
- importjavax.xml.namespace.QName;
- importorg.apache.axis2.AxisFault;
- importorg.apache.axis2.addressing.EndpointReference;
- importorg.apache.axis2.client.Options;
- importorg.apache.axis2.rpc.client.RPCServiceClient;
- publicclassCalculateServiceTest{
- /**
- *@paramargs
- *@throwsAxisFault
- */
- publicstaticvoidmain(String[]args)throwsAxisFault{
- //TODOAuto-generatedmethodstub
- //使用RPC方式调用WebService
- RPCServiceClientserviceClient=newRPCServiceClient();
- Optionsoptions=serviceClient.getOptions();
- //指定调用WebService的URL
- EndpointReferencetargetEPR=newEndpointReference(
- "http://localhost:8080/WebServiceTest1/services/CalculateService");
- options.setTo(targetEPR);
- //指定要调用的计算机器中的方法及WSDL文件的命名空间:edu.sjtu.webservice。
- QNameopAddEntry=newQName("http://webservice.sjtu.edu","plus");//加法
- QNameopAddEntryminus=newQName("http://webservice.sjtu.edu","minus");//减法
- QNameopAddEntrymultiply=newQName("http://webservice.sjtu.edu","multiply");//乘法
- QNameopAddEntrydivide=newQName("http://webservice.sjtu.edu","divide");//除法
- //指定plus方法的参数值为两个,分别是加数和被加数
- Object[]opAddEntryArgs=newObject[]{1,2};
- //指定plus方法返回值的数据类型的Class对象
- Class[]classes=newClass[]{float.class};
- //调用plus方法并输出该方法的返回值
- System.out.println(serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs,classes)[0]);
- System.out.println(serviceClient.invokeBlocking(opAddEntryminus,opAddEntryArgs,classes)[0]);
- System.out.println(serviceClient.invokeBlocking(opAddEntrymultiply,opAddEntryArgs,classes)[0]);
- System.out.println(serviceClient.invokeBlocking(opAddEntrydivide,opAddEntryArgs,classes)[0]);
- }
- }
- 3.0
- -1.0
- 2.0
- 0.5
- 3.0
- -1.0
- 2.0
- 0.5
3.实例2.HelloService
(1)首先定义服务方法,代码如下所示:
- packageedu.sjtu.webservice;
- publicclassHelloService{
- publicStringsayHelloNew(){
- return"hello";
- }
- publicStringsayHelloToPersonNew(Stringname){
- if(name==null){
- name="nobody";
- }
- return"hello,"+name;
- }
- publicvoidupdateData(Stringdata){
- System.out.println(data+"已更新。");
- }
- }
- packageedu.sjtu.webservice;
- publicclassHelloService{
- publicStringsayHelloNew(){
- return"hello";
- }
- publicStringsayHelloToPersonNew(Stringname){
- if(name==null){
- name="nobody";
- }
- return"hello,"+name;
- }
- publicvoidupdateData(Stringdata){
- System.out.println(data+"已更新。");
- }
- }
(3)编写客户端代码调用WebService(主要参考[5])
本文例子与其他例子最大的不同就在这里,其他例子一般需要根据刚才的服务wsdl生成客户端stub,然后通过stub来调用服务,这种方式显得比较单一,客户端必须需要stub存根才能够访问服务,很不方面。本例子的客户端不采用stub方式,而是一种实现通用的调用方式,不需要任何客户端存根即可访问服务。只需要指定对于的web servce地址、操作名、参数和函数返回类型即可。代码如下所示:
HelloServiceTest2.java
- packageedu.sjtu.webservice.test;
- importjavax.xml.namespace.QName;
- importorg.apache.axis2.AxisFault;
- importorg.apache.axis2.addressing.EndpointReference;
- importorg.apache.axis2.client.Options;
- importorg.apache.axis2.rpc.client.RPCServiceClient;
- publicclassHelloServiceTest2{
- privateRPCServiceClientserviceClient;
- privateOptionsoptions;
- privateEndpointReferencetargetEPR;
- publicHelloServiceTest2(Stringendpoint)throwsAxisFault{
- serviceClient=newRPCServiceClient();
- options=serviceClient.getOptions();
- targetEPR=newEndpointReference(endpoint);
- options.setTo(targetEPR);
- }
- publicObject[]invokeOp(StringtargetNamespace,StringopName,
- Object[]opArgs,Class<?>[]opReturnType)throwsAxisFault,
- ClassNotFoundException{
- //设定操作的名称
- QNameopQName=newQName(targetNamespace,opName);
- //设定返回值
- //Class<?>[]opReturn=newClass[]{opReturnType};
- //操作需要传入的参数已经在参数中给定,这里直接传入方法中调用
- returnserviceClient.invokeBlocking(opQName,opArgs,opReturnType);
- }
- /**
- *@paramargs
- *@throwsAxisFault
- *@throwsClassNotFoundException
- */
- publicstaticvoidmain(String[]args)throwsAxisFault,
- ClassNotFoundException{
- //TODOAuto-generatedmethodstub
- finalStringendPointReference="http://localhost:8080/WebServiceTest1/services/HelloService";
- finalStringtargetNamespace="http://webservice.sjtu.edu";
- HelloServiceTest2client=newHelloServiceTest2(endPointReference);
- StringopName="sayHelloToPersonNew";
- Object[]opArgs=newObject[]{"MyFriends"};
- Class<?>[]opReturnType=newClass[]{String[].class};
- Object[]response=client.invokeOp(targetNamespace,opName,opArgs,
- opReturnType);
- System.out.println(((String[])response[0])[0]);
- }
- }
- packageedu.sjtu.webservice.test;
- importjavax.xml.namespace.QName;
- importorg.apache.axis2.AxisFault;
- importorg.apache.axis2.addressing.EndpointReference;
- importorg.apache.axis2.client.Options;
- importorg.apache.axis2.rpc.client.RPCServiceClient;
- publicclassHelloServiceTest2{
- privateRPCServiceClientserviceClient;
- privateOptionsoptions;
- privateEndpointReferencetargetEPR;
- publicHelloServiceTest2(Stringendpoint)throwsAxisFault{
- serviceClient=newRPCServiceClient();
- options=serviceClient.getOptions();
- targetEPR=newEndpointReference(endpoint);
- options.setTo(targetEPR);
- }
- publicObject[]invokeOp(StringtargetNamespace,StringopName,
- Object[]opArgs,Class<?>[]opReturnType)throwsAxisFault,
- ClassNotFoundException{
- //设定操作的名称
- QNameopQName=newQName(targetNamespace,opName);
- //设定返回值
- //Class<?>[]opReturn=newClass[]{opReturnType};
- //操作需要传入的参数已经在参数中给定,这里直接传入方法中调用
- returnserviceClient.invokeBlocking(opQName,opArgs,opReturnType);
- }
- /**
- *@paramargs
- *@throwsAxisFault
- *@throwsClassNotFoundException
- */
- publicstaticvoidmain(String[]args)throwsAxisFault,
- ClassNotFoundException{
- //TODOAuto-generatedmethodstub
- finalStringendPointReference="http://localhost:8080/WebServiceTest1/services/HelloService";
- finalStringtargetNamespace="http://webservice.sjtu.edu";
- HelloServiceTest2client=newHelloServiceTest2(endPointReference);
- StringopName="sayHelloToPersonNew";
- Object[]opArgs=newObject[]{"MyFriends"};
- Class<?>[]opReturnType=newClass[]{String[].class};
- Object[]response=client.invokeOp(targetNamespace,opName,opArgs,
- opReturnType);
- System.out.println(((String[])response[0])[0]);
- }
- }
(4)改写客户端调用服务的代码
(3)中提到的客户端应用代码写的略微有些繁杂,下面将上面的客户端调用service程序进行改写,简洁了许多。代码如下:
HelloServiceTest.java
- importjavax.xml.namespace.QName;
- importorg.apache.axis2.AxisFault;
- importorg.apache.axis2.addressing.EndpointReference;
- importorg.apache.axis2.client.Options;
- importorg.apache.axis2.rpc.client.RPCServiceClient;
- publicclassHelloServiceTest{
- publicstaticvoidmain(Stringargs[])throwsAxisFault{
- //使用RPC方式调用WebService
- RPCServiceClientserviceClient=newRPCServiceClient();
- Optionsoptions=serviceClient.getOptions();
- //指定调用WebService的URL
- EndpointReferencetargetEPR=newEndpointReference("http://localhost:8080/WebServiceTest1/services/HelloService");
- options.setTo(targetEPR);
- //指定要调用的sayHelloToPerson方法及WSDL文件的命名空间
- QNameopAddEntry=newQName("http://webservice.sjtu.edu","sayHelloToPersonNew");
- //指定sayHelloToPerson方法的参数值
- Object[]opAddEntryArgs=newObject[]{"xuwei"};
- //指定sayHelloToPerson方法返回值的数据类型的Class对象
- Class[]classes=newClass[]{String.class};
- //调用sayHelloToPerson方法并输出该方法的返回值
- System.out.println(serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs,classes)[0]);
- }
- }
- importjavax.xml.namespace.QName;
- importorg.apache.axis2.AxisFault;
- importorg.apache.axis2.addressing.EndpointReference;
- importorg.apache.axis2.client.Options;
- importorg.apache.axis2.rpc.client.RPCServiceClient;
- publicclassHelloServiceTest{
- publicstaticvoidmain(Stringargs[])throwsAxisFault{
- //使用RPC方式调用WebService
- RPCServiceClientserviceClient=newRPCServiceClient();
- Optionsoptions=serviceClient.getOptions();
- //指定调用WebService的URL
- EndpointReferencetargetEPR=newEndpointReference("http://localhost:8080/WebServiceTest1/services/HelloService");
- options.setTo(targetEPR);
- //指定要调用的sayHelloToPerson方法及WSDL文件的命名空间
- QNameopAddEntry=newQName("http://webservice.sjtu.edu","sayHelloToPersonNew");
- //指定sayHelloToPerson方法的参数值
- Object[]opAddEntryArgs=newObject[]{"xuwei"};
- //指定sayHelloToPerson方法返回值的数据类型的Class对象
- Class[]classes=newClass[]{String.class};
- //调用sayHelloToPerson方法并输出该方法的返回值
- System.out.println(serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs,classes)[0]);
相关推荐
eclipse开发的一个webservice简单实例源码,下载可以直接运行。
Eclipse开发WebService服务端实例。开发客户端代码。超级简单易懂的实例。
主要介绍了Eclipse+Webservice简单开发实例的相关资料,需要的朋友可以参考下
利用Java编写简单的WebService实例 Axis2与Eclipse整合开发Web Service
Java WebService服务端开发。Java WebService客户端开发。java Eclipse + WebService 简单实例。简单易懂,容易上手,适合初学者,测试学习。
Eclipse 开发 Axis_WebService 实例总结
用eclipse+myeclipse 开发webservice步骤和客户端调用实例.
使用Axis编写WebService比较简单,就我的理解,WebService的实现代码和编写Java代码其实没有什么区别,主要是将哪些Java类发布为WebService。下面是一个从编写测试例子到发布WebService,以及编写测试代码的过程介绍...
本文介绍了Eclipse3.2+MyEclipse5.5M1+axis2_1.1.1+tomcat5.5 开发Web Services的一个实例,相当详细!!
用Eclipse开发,一开始就会想到xfire,它是集成与Eclipse的.所以 开发起来非常方便,现在我们来看看axis的开发是怎么样的? 看完本文后,你可以相互比较一下xfire与它的差别. 首先,创建axis环境 .(注:我事先说明.这是...
这是一个完整的在ECLIPSE下利用Xfire框架开发的一年webservice所用的数据库为sqlserver2005.下载后只要工程导入ECLIPSE中即可.
网上很流行的一个文档,axis的一个helloworld开发,源文档中有四处错误,经本人修改后重新发布,eclipse调试通过
Eclipse下Struts、CVS的应用以及数据库构建和WebService应用开发;Eclipse下的Hibernate、Spring及其他插件的应用和开发;电子商务系统、管理信息系统、文件审批系统的Web开发。 本书内容循序渐进,实例丰富,操作及...
是eclipse导出的war文件,包含了资源。开发环境jdk1.7 tomcat7。 通过了测试,对应的文档:http://blog.csdn.net/biboheart/article/details/9148651, 有文档了,这里就不多说了。
java基于axis的webservice实例,包含服务器端和客户端,开发工具为eclipse,下载后将TestServiceLocator类的地址改成你服务器端所在的机器ip地址,然后部署到tomcat中部署既可以运行。有问题可以留言
Eclipse下Struts、CVS的应用以及数据库构建和WebService应用开发;Eclipse下的Hibernate、Spring及其他插件的应用和开发;电子商务系统、管理信息系统、文件审批系统的Web开发。 本书内容循序渐进,实例丰富,操作...
这是本人练习调用webservice接口的实例:通过省份获取城市,在获取天气信息.一切说明都在附件中,直接下载导入到eclipse,开发环境正常,并且能上网,即可运行成功。希望能帮到需要的小伙伴。
运用myeclipse+axis实现的webService小例子,希望能提供点帮助
CXF教程.doc Eclipse中用wsdl生成java客户端.docx java 利用Xfire 来生成Webservice 的WSDL文件 - 爱琴海 - ITeye技术网站.htm XFire部署开发webservice.docx XFire建立Webservice实例.doc XFire开发WebService详解....
提供流程定义、流程设置、流程版本更变,流程任务管理,流程实例管理,流程表单管理,流程表单在线义、模板表单,流程历史查询,查看我的任务,任务回退等丰富流程开发管理。 9 人力管理 10 报表管理 11 公文管理 ...