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

简介       

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

Axis2下载和部署

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

       下载地址:web

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

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

       访问http://localhost:8080/axis2/,若是看到欢迎主页,则说明部署成功。服务器

配置Axis2

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

 

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

 

编写服务

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

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

0配置POJO发布服务

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

       至此,咱们已经成功的建立了一个WebService服务了,so easy!webapp

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

 

[java]  view plain  copy
 
 print?在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
 
 print?在CODE上查看代码片派生到个人代码片
  1. <ns:sayHelloResponse xmlns:ns="http://ws.apache.org/axis2">  
  2. <return>hello</return>  
  3. </ns:sayHelloResponse>  

       这里有两点须要注意:

 

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

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

 

[html]  view plain  copy
 
 print?在CODE上查看代码片派生到个人代码片
  1. <deployer extension=".class" directory="<要增长的目录名称>" class="org.apache.axis2.deployment.POJODeployer" />  

         要注意多个目录见WebService要惟一,不然会重名,重名后,先部署的会成功,后部署的会报错。

services.xml配置文件发布服务

       虽然POJO的方式不须要配置文件,可是其服务类不能放在包内,显然是不符合咱们平常开发的,Axis2也容许带包的类发布WebService,若是不容许,估计就没人用了。

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

 

[java]  view plain  copy
 
 print?在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
 
 print?在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
 
 print?在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
 
 print?在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
 
 print?在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[] {1, 2};  
  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
 
 print?在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
 
 print?在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
 
 print?在CODE上查看代码片派生到个人代码片
  1. wsdl2java -uri http://www.webxml.com.cn/WebServices/RandomFontsWebService.asmx?wsdl -p webservice.client.test -s  

       调用该类

 

 

[java]  view plain  copy
 
 print?在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. }  
相关文章
相关标签/搜索