Java创建WebService服务及客户端实现

来源:互联网 发布:腾讯软件管家 编辑:程序博客网 时间:2024/05/01 08:44
 分类:

目录(?)[+]

简介       

       WebService是一种服务的提供方式,通过WebService,不同应用间相互间调用变的很方便,网络上有很多常用的WebService服务,如:http://developer.51cto.com/art/200908/147125.htm,不同的语言平台对WebService都有实现,Java的WebService实现,比较流行的有Axis2、Jaxws,本文介绍的是Axis2。

Axis2下载和部署

       Axis2是Apache开发的一个开源项目,再次感叹Apache的伟大!

       下载地址:

       http://mirror.bit.edu.cn/apache/axis/axis2/java/core/1.6.2/axis2-1.6.2-war.zip

       将其内axis2.war解压到<Tomcat安装目录>/webapps下,启动Tomcat,war包会自动解压,

       访问http://localhost:8080/axis2/,如果看到欢迎主页,则说明部署成功。

配置Axis2

       <Tomcat安装目录>/webapps/axis2/WEB-INF/conf/axis2.xml,配置其内两个属性,以便调试。

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <parameter name="hotdeployment">true</parameter><!-- 开启热部署,不需要重启即可部署服务 -->  
  2. <parameter name="hotupdate">true</parameter><!-- 开启热更新,不需要重启即可更新服务 -->  

编写服务

       所谓服务就是编写一个类,写一些方法,方法返回数据,WebService客户端获取数据。

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. public class HelloService {  
  2.   
  3.     public String sayHello() {  
  4.         return "hello";  
  5.     }  
  6.       
  7. }  

零配置发布服务

       服务类创建好后,我们需要发布到服务器上,将HelloService.class放到<Tomcat安装目录>/webapps/axis2/WEB-INF/pojo下,pojo没有需要创建。

       至此,我们已经成功的创建了一个WebService服务了,so easy!

       再次访问http://localhost:8080/axis2/,点击Services,可以发现可用services中多了一个HelloService,其内有一个可用操作sayHello,说明发布成功。

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. HelloService  
  2.   
  3. Service Description : No description available for this service  
  4.   
  5. Service EPR : http://localhost:8080/axis2/services/HelloService  
  6.   
  7. Service Status : Active  
  8.   
  9.   
  10. Available Operations  
  11. sayHello  
       访问http://localhost:8080/axis2/services/HelloService,页面输出正是我们的返回值。

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <ns:sayHelloResponse xmlns:ns="http://ws.apache.org/axis2">  
  2. <return>hello</return>  
  3. </ns:sayHelloResponse>  
       这里有两点需要注意:

       - 发布的类不能放在包里,既不能使用package关键字;

       - 默认的发布目录是pojo,可以在<Tomcat安装目录>/webapps/axis2/WEB-INF/conf/axis2.xml中增加目录,

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <deployer extension=".class" directory="<要增加的目录名称>" class="org.apache.axis2.deployment.POJODeployer" />  
         要注意多个目录间WebService要唯一,否则会重名,重名后,先部署的会成功,后部署的会报错。

services.xml配置文件发布服务

       虽然上面的方式不需要配置文件,但是其服务类不能放在包内,显然是不符合我们日常开发的,Axis2也允许带包的类发布WebService,如果不允许,估计就没人用了。

       首先写一个较复杂的服务类,多个方法,带参数,有返回值的。

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package webservice.test;  
  2.   
  3. /** 
  4.  * 计算器运算 
  5.  *  
  6.  * @author gaoshuang 
  7.  */  
  8. public class CalculateService {  
  9.   
  10.     // 加法  
  11.     public float plus(float x, float y) {  
  12.         return x + y;  
  13.     }  
  14.   
  15.     // 减法  
  16.     public float minus(float x, float y) {  
  17.         return x - y;  
  18.     }  
  19.   
  20.     // 乘法  
  21.     public float multiply(float x, float y) {  
  22.         return x * y;  
  23.     }  
  24.   
  25.     // 除法  
  26.     public float divide(float x, float y) {  
  27.         if (y != 0)  
  28.             return x / y;  
  29.         else  
  30.             return -1;  
  31.     }  
  32. }  
       然后编写services.xml,该文件需要放在META-INF文件夹下。

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!-- 服务名称 -->  
  3. <service name="CalculateService">  
  4.     <!-- 服务描述 -->  
  5.     <description>  
  6.         加减乘除计算服务  
  7.     </description>  
  8.     <!-- 设置服务类 -->  
  9.     <parameter name="ServiceClass">  
  10.         com.runqianapp.webservice.test.CalculateService  
  11.     </parameter>  
  12.     <!-- 方法 -->  
  13.     <operation name="plus">  
  14.         <!-- 方法处理器,RPCMessageReceiver为带返回值的处理器,  
  15.                      RPCInOnlyMessageReceiver为不带返回值的处理器 -->  
  16.         <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"  
  17.             class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />  
  18.     </operation>  
  19.     <operation name="minus">  
  20.         <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"  
  21.             class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />  
  22.     </operation>  
  23.     <operation name="multiply">  
  24.         <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"  
  25.             class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />  
  26.     </operation>  
  27.     <operation name="divide">  
  28.         <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"  
  29.             class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />  
  30.     </operation>  
  31. </service>  

       最后将这两个文件打成jar包,不论用工具还是手动打,打的都是最外层的文件夹。

       

       我打的名字是server.jar,更改后缀为aar,所以最后是server.aar,Axis2建议使用aar发布WebService,

       将server.aar放到<Tomcat安装目录>/webapps/axis2/WEB-INF/services下,访问http://localhost:8080/axis2/services/listServices,

       多出了一个CalculateService,说明发布成功。

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. CalculateService  
  2.   
  3. Service Description : CalculateService  
  4.   
  5. Service EPR : http://localhost:8080/axis2/services/CalculateService  
  6.   
  7. Service Status : Active  
  8.   
  9.   
  10. Available Operations  
  11. divide  
  12. plus  
  13. minus  
  14. multiply  
       分别访问

       http://localhost:8080/axis2/services/CalculateService/plus?x=1&y=2

       http://localhost:8080/axis2/services/CalculateService/divide?x=1&y=2

       http://localhost:8080/axis2/services/CalculateService/minus?x=1&y=2

       http://localhost:8080/axis2/services/CalculateService/multiply?x=1&y=2
       也可以发布多个WebService,可以使用serviceGroup标签。

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <serviceGroup>  
  2. <service name="myService1">  
  3.     ...  
  4. </service>  
  5. <service name="myService2">  
  6.     ...  
  7. </service>  
  8. </serviceGroup>  

客户端实现

       以上介绍的都是WebService服务创建及发布,那么有了一个WebService服务后,我们如何调用呢?只在浏览器上访问是没有意义的。

       下载Axis2客户端压缩包:http://mirror.esocc.com/apache/axis/axis2/java/core/1.6.2/axis2-1.6.2-bin.zip,并解压。

       新建工程WebServiceClientTest,将<Axis2客户端安装目录>/lib下所有jar包添加到工程中;

       编写客户端代码;

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package webservice.client.test;  
  2.   
  3. import javax.xml.namespace.QName;  
  4.   
  5. import org.apache.axis2.AxisFault;  
  6. import org.apache.axis2.addressing.EndpointReference;  
  7. import org.apache.axis2.client.Options;  
  8. import org.apache.axis2.rpc.client.RPCServiceClient;  
  9.   
  10. public class Client1 {  
  11.   
  12.     /** 
  13.      * @param args 
  14.      * @throws AxisFault 
  15.      */  
  16.     public static void main(String[] args) throws AxisFault {  
  17.         // 使用RPC方式调用WebService  
  18.         RPCServiceClient serviceClient = new RPCServiceClient();  
  19.         Options options = serviceClient.getOptions();  
  20.         // 指定调用WebService的URL  
  21.         EndpointReference targetEPR = new EndpointReference(  
  22.                 "http://localhost:8080/axis2/services/CalculateService");  
  23.         options.setTo(targetEPR);  
  24.         // 调用方法的参数值  
  25.         Object[] entryArgs = new Object[] {12};  
  26.         // 调用方法返回值的数据类型的Class对象  
  27.         Class[] classes = new Class[] { float.class };  
  28.         // 调用方法名及WSDL文件的命名空间  
  29.         // 命名空间是http://localhost:8080/axis2/services/CalculateService?wsdl中wsdl:definitions标签targetNamespace属性  
  30.         QName opName = new QName("http://test.webservice""plus");  
  31.         // 执行方法获取返回值  
  32.         // 没有返回值的方法使用serviceClient.invokeRobust(opName, entryArgs)  
  33.         Object result = serviceClient.invokeBlocking(opName, entryArgs, classes)[0];  
  34.         System.out.println(result);  
  35.         // out: 3.0  
  36.     }  
  37.   
  38. }  
       以上是实现了一个简单的WebSerivce客户端,调用CalculateService中的plus方法,由代码可见,这种调用方式比较杂乱,代码不太友好。

wsdl2java简化客户端

       <Axis2客户端安装目录>/bin目录,其内有两个bat,wsdl2java.bat和java2wsdl.bat,可以实现WSDL文件和Java之间的互相转换。

       考虑到我们以后可能经常使用这些命令,设置环境变量,方便以后调用。在系统变量中加入AXIS2_HOME=<Axis2客户端安装目录>,path中追加;%AXIS2_HOME%\bin。

       启动命令提示符,进入WebServiceTestClient所在目录,运行

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. wsdl2java -uri http://localhost:8080/axis2/services/CalculateService?wsdl -p webservice.client.test -s  
       参数说明:uri - wsdl文件路径,网络路径或本地路径,p - 打包,这里和上一个客户端实现类打在了一个包里,wsdl2java有很多参数,详细可以运行该命令去查看。

       执行后,如果没有报错,说明运行成功,刷新项目,该包下多出了一个CalculateServiceStub类,里面的代码极其复杂,还乱呼呼的,这我们不用管,调用该类。

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package webservice.client.test;  
  2.   
  3. import java.rmi.RemoteException;  
  4.   
  5. import webservice.client.test.CalculateServiceStub.Plus;  
  6.   
  7. public class Client2 {  
  8.   
  9.     /** 
  10.      * @param args 
  11.      * @throws RemoteException  
  12.      */  
  13.     public static void main(String[] args) throws RemoteException {  
  14.         CalculateServiceStub stub = new CalculateServiceStub();  
  15.         Plus plus = new Plus();  
  16.         plus.setX(1);  
  17.         plus.setY(2);  
  18.         float result = stub.plus(plus).get_return();// 返回值自动转型,这也是强大之处  
  19.         System.out.println(result);  
  20.     }  
  21.   
  22. }  

       如此做的好处就是调用时不需要在去查看WSDL,和正常使用一个类一样,对WebService的封装都由wsdl2java自动生成,代码更优雅、简洁。

利用wsdl2java轻松使用第三方WebService服务

       有了wsdl2java,已知一个WSDL文件我们就可以轻松的生成WebService客户端供我们调用,给我们服务。文章开头给出的链接包含了一些第三方服务,有一个服务是生成随机个数中文,WSDL:http://www.webxml.com.cn/WebServices/RandomFontsWebService.asmx?wsdl,同样,启动命令提示符,进入项目路径,执行

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. wsdl2java -uri http://www.webxml.com.cn/WebServices/RandomFontsWebService.asmx?wsdl -p webservice.client.test -s  
       调用该类

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package webservice.client.test;  
  2.   
  3. import java.rmi.RemoteException;  
  4.   
  5. import webservice.client.test.RandomFontsWebServiceStub.ArrayOfString;  
  6. import webservice.client.test.RandomFontsWebServiceStub.GetChineseFonts;  
  7.   
  8. public class ThirdClient {  
  9.   
  10.     /** 
  11.      * @param args 
  12.      * @throws RemoteException  
  13.      */  
  14.     public static void main(String[] args) throws RemoteException {  
  15.         RandomFontsWebServiceStub stub = new RandomFontsWebServiceStub();  
  16.         GetChineseFonts getChineseFonts = new GetChineseFonts();  
  17.         getChineseFonts.setByFontsLength(10);// 免费使用有限制,最多8个  
  18.         ArrayOfString result = stub.getChineseFonts(getChineseFonts).getGetChineseFontsResult();  
  19.         for(String str : result.getString()) {  
  20.             System.out.println(str);  
  21.         }  
  22.     }  
  23.   
  24. }  

源码下载

       文中代码尽在下面链接中,免积分下载。

       http://download.csdn.net/download/ghsau/6400843

       (完)

       本文来自:高爽|Coder,原文地址:http://blog.csdn.net/ghsau/article/details/12714965

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 软件与系统不兼容怎么办 软件与手机系统不兼容怎么办 qq和手机不兼容怎么办 来个软件不兼容怎么办 安卓8.0不兼容app怎么办 两条内存不兼容怎么办 王者荣耀软件不兼容怎么办 冒险岛不兼容win7怎么办 百度网盘手机号换了怎么办 破解版游戏闪退怎么办 安卓手机软件不兼容怎么办 安卓8.0软件闪退怎么办 游戏与手机系统不兼容怎么办 耳机和手机不兼容怎么办 软件和手机不兼容怎么办 小米6开关键失灵怎么办 同步助手下载不了微信旧版本怎么办 闲鱼退货卖家拒收怎么办 闲鱼把联系人删了怎么办 闲鱼付款了卖家不发货怎么办 红米4c卡怎么办 如果买鞋子买到假的怎么办 猎趣永久封号钱怎么办 支付宝换绑定手机后怎么办 为什么回收站的删除键不见了怎么办 微信在异地登录怎么办 支付宝帐号被冻结怎么办 进不了路由器设置页面怎么办 支付宝支付密码忘记了怎么办 淘宝忘记登录密码了怎么办 手机换卡了微信怎么办 淘宝退款成功后收到货怎么办 没收到货退款商家不处理怎么办 没收到货申请退款卖家不处理怎么办 京东已收到货却不处理退款怎么办 货退了卖家不退款怎么办 淘宝退了货卖家不退款怎么办 手机淘宝不显示图片怎么办 京东申请退款卖家不处理怎么办 企业网银冻结了怎么办 农行k宝坏了怎么办