renxiao2003 发表于 2011-12-21 08:44

用POJO实现0配置的WebService

<DIV>
<DIV class=postBody sizset="0" sizcache="2">
<P>Axis2是一套崭新的WebService引擎,该版本是对Axis1.x重新设计的产物。Axis2不仅支持SOAP1.1和SOAP1.2,还集成了非常流行的REST WebService,同时还支持Spring、JSON等技术。这些都将在后面的系列教程中讲解。在本文中主要介绍了如何使用Axis2开发一个不需要任何配置文件的WebService,并在客户端使用Java和C#调用这个WebService。<STRONG>一、Axis2</STRONG><STRONG>的下载和安装</STRONG><BR>&nbsp; &nbsp;读者可以从如下的网址下载Axis2的最新版本:<BR>&nbsp; &nbsp;http://ws.apache.org/axis2/</P>
<P>&nbsp; &nbsp;在本文使用了目前Axis2的最新版本1.4.1。读者可以下载如下两个zip包:<BR>&nbsp; &nbsp; axis2-1.4.1-bin.zip<BR>&nbsp; &nbsp; axis2-1.4.1-war.zip<BR>&nbsp; &nbsp;其中axis2-1.4.1-bin.zip文件中包含了Axis2中所有的jar文件, axis2-1.4.1-war.zip文件用于将WebService发布到Web容器中。<BR>&nbsp; &nbsp;将axis2-1.4.1-war.zip文件解压到相应的目录,将目录中的axis2.war文件放到&lt;Tomcat安装目录&gt;\webapps目录中(本文使用的Tomcat的版本是6.x),并启动Tomcat。<BR>&nbsp; &nbsp;在浏览器地址栏中输入如下的URL:<BR>&nbsp; &nbsp; http:/ /localhost:8080/axis2/</P>
<P>&nbsp; &nbsp;如果在浏览器中显示出如图1所示的页面,则表示Axis2安装成功。<BR></P>
<P style="TEXT-ALIGN: center" align=center sizset="5" sizcache="0">
<DIV class=tc><IMG style="WIDTH: 600px" alt=(1):用POJO实现0配置的WebService src="http://img.huomo.cn/article/f2d/78972/1.jpg"></DIV>
<P></P>
<P style="TEXT-ALIGN: center">图1</P>
<P><STRONG>二、编写和发布WebService</STRONG><BR>&nbsp;&nbsp;对于用Java实现的服务程序给人的印象就是需要进行大量的配置,不过这一点在Axis2中将被终结。在Axis2中不需要进行任何的配置,就可以直接将一个简单的POJO发布成WebService。其中POJO中所有的public方法将被发布成WebService方法。<BR>&nbsp; &nbsp;下面我们来实现一个简单的POJO,代码如下:</P>
<UL>
<LI>public class SimpleService
<LI>{
<LI>&nbsp; &nbsp; public String getGreeting(String name)
<LI>&nbsp; &nbsp; {
<LI>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;return "你好 " + name;
<LI>&nbsp; &nbsp; }&nbsp; &nbsp;
<LI>&nbsp; &nbsp; public int getPrice()
<LI>&nbsp; &nbsp; {
<LI>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;return new java.util.Random().nextInt(1000);
<LI>&nbsp; &nbsp; }&nbsp; &nbsp;
<LI>} </LI></UL>
<P>复制代码</P>
<P>&nbsp; &nbsp;在SimpleService类中有两个方法,由于这两个方法都是public方法,因此,它们都将作为WebService方法被发布。<BR>&nbsp; &nbsp;编译SimpleService类后,将SimpleService.class文件放到&lt;Tomcat安装目录&gt;\webapps\axis2\WEB-INF\pojo目录中(如果没有pojo目录,则建立该目录)。现在我们已经成功将SimpleService类发布成了WebService。在浏览器地址栏中输入如下的URL:<BR>http:/ /localhost:8080/axis2/services/listServices</P>
<P>&nbsp; &nbsp;这时当前页面将显示所有在Axis2中发布的WebService,如图2所示。</P>
<P style="TEXT-ALIGN: center" align=center sizset="6" sizcache="0">
<DIV class=tc><IMG style="WIDTH: 600px" alt=(1):用POJO实现0配置的WebService src="http://img.huomo.cn/article/f2d/78972/2.jpg"></DIV>图2
<P></P>
<P>&nbsp; &nbsp; 在浏览器地址栏中输入如下的两个URL来分别测试getGreeting和getPrice方法:<BR>http:/ /localhost:8080/axis2/services/SimpleService/getGreeting?name=bill</P>
<P>http:/ /localhost:8080/axis2/services/SimpleService/getPrice</P>
<P>&nbsp; &nbsp;图3和图4分别显示了getGreeting和getPrice方法的测试结果。</P>
<P style="TEXT-ALIGN: center" align=center sizset="7" sizcache="0">
<DIV class=tc><IMG style="WIDTH: auto" alt=(1):用POJO实现0配置的WebService src="http://img.huomo.cn/article/f2d/78972/3.jpg"></DIV>
<P></P>
<P align=center>图3&nbsp;&nbsp;getGreeting方法的测试结果</P>
<P style="TEXT-ALIGN: center" align=center sizset="8" sizcache="0">
<DIV class=tc><IMG style="WIDTH: auto" alt=(1):用POJO实现0配置的WebService src="http://img.huomo.cn/article/f2d/78972/4.jpg"></DIV>
<P></P>
<P style="TEXT-ALIGN: center" align=center>图4&nbsp;&nbsp;getPrice方法的测试结果</P>
<P>&nbsp; &nbsp;在编写、发布和测试0配置的WebService时应注意如下几点:</P>
<P>1. POJO类不能使用package关键字声明包。</P>
<P>2. Axis2在默认情况下可以热发布WebService,也就是说,将WebService的.class文件复制到pojo目录中时,Tomcat不需要重新启动就可以自动发布WebService。如果想取消Axis2的热发布功能,可以打开&lt;Tomcat安装目录&gt;\webapps\axis2\WEB-INF\conf\axis2.xml,找到如下的配置代码:</P>
<UL>
<LI>&lt;parameter name="hotdeployment"&gt;true&lt;/parameter&gt; </LI></UL>
<P>复制代码</P>
<P>&nbsp; &nbsp;将true改为false即可。要注意的是,Axis2在默认情况下虽然是热发布,但并不是热更新,也就是说,一旦成功发布了WebService,再想更新该WebService,就必须重启Tomcat。这对于开发人员调试WebService非常不方便,因此,在开发WebService时,可以将Axis2设为热更新。在axis2.xml文件中找到&lt;parametername="hotupdate"&gt;false&lt;/parameter&gt;,将false改为true即可。</P>
<P>3. 在浏览器中测试WebService时,如果WebService方法有参数,需要使用URL的请求参数来指定该WebService方法参数的值,请求参数名与方法参数名要一致,例如,要测试getGreeting方法,请求参数名应为name,如上面的URL所示。</P>
<P>4. 发布WebService的pojo目录只是默认的,如果读者想在其他的目录发布WebService,可以打开axis2.xml文件,并在&lt;axisconfig&gt;元素中添加如下的子元素:</P>
<UL>
<LI>&nbsp; &nbsp;&lt;deployer extension=".class" directory="my" class="org.apache.axis2.deployment.POJODeployer"/&gt; </LI></UL>
<P>复制代码</P>
<P>&nbsp; &nbsp;上面的配置允许在&lt;Tomcat安装目录&gt;\webapps\axis2\WEB-INF\my目录中发布WebService。例如,将本例中的SimpleService.class复制到my目录中也可以成功发布(但要删除pojo目录中的SimpleService.class,否则WebService会重名)。</P>
<P><STRONG>三、</STRONG> <STRONG>用Java</STRONG><STRONG>实现调用WebService</STRONG><STRONG>的客户端程序</STRONG><BR>&nbsp; &nbsp; WebService是为程序服务的,只在浏览器中访问WebService是没有意义的。因此,在本节使用Java实现了一个控制台程序来调用上一节发布的WebService。调用WebService的客户端代码如下:</P>
<UL>
<LI>package client;
<LI>
<LI>import javax.xml.namespace.QName;
<LI>import org.apache.axis2.addressing.EndpointReference;
<LI>import org.apache.axis2.client.Options;
<LI>import org.apache.axis2.rpc.client.RPCServiceClient;
<LI>
<LI>public class RPCClient
<LI>{
<LI>&nbsp; &nbsp; public static void main(String[] args) throws Exception&nbsp;&nbsp;
<LI>&nbsp; &nbsp; {
<LI>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;使用RPC方式调用WebService&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
<LI>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;RPCServiceClient serviceClient = new RPCServiceClient();
<LI>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Options options = serviceClient.getOptions();
<LI>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;指定调用WebService的URL
<LI>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;EndpointReference targetEPR = new EndpointReference(
<LI>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; "http://localhost:8080/axis2/services/SimpleService");
<LI>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;options.setTo(targetEPR);
<LI>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;指定getGreeting方法的参数值
<LI>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Object[] opAddEntryArgs = new Object[] {"超人"};
<LI>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;指定getGreeting方法返回值的数据类型的Class对象
<LI>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Class[] classes = new Class[] {String.class};
<LI>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;指定要调用的getGreeting方法及WSDL文件的命名空间
<LI>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;QName opAddEntry = new QName("http://ws.apache.org/axis2", "getGreeting");
<LI>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;调用getGreeting方法并输出该方法的返回值
<LI>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;System.out.println(serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes));
<LI>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;下面是调用getPrice方法的代码,这些代码与调用getGreeting方法的代码类似
<LI>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;classes = new Class[] {int.class};
<LI>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;opAddEntry = new QName("http://ws.apache.org/axis2", "getPrice");
<LI>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;System.out.println(serviceClient.invokeBlocking(opAddEntry, new Object[]{}, classes));
<LI>&nbsp; &nbsp; }
<LI>} </LI></UL>
<P>复制代码</P>
<P>运行上面的程序后,将在控制台输出如下的信息:<BR></P>你好 超人<BR>443
<P>&nbsp; &nbsp;在编写客户端代码时应注意如下几点:</P>
<P>1. 客户端代码需要引用很多Axis2的jar包,如果读者不太清楚要引用哪个jar包,可以在Eclipse的工程中引用Axis2发行包的lib目录中的所有jar包。</P>
<P>2. 在本例中使用了RPCServiceClient类的invokeBlocking方法调用了WebService中的方法。invokeBlocking方法有三个参数,其中第一个参数的类型是QName对象,表示要调用的方法名;第二个参数表示要调用的WebService方法的参数值,参数类型为Object[];第三个参数表示WebService方法的返回值类型的Class对象,参数类型为Class[]。当方法没有参数时,invokeBlocking方法的第二个参数值不能是null,而要使用new Object[]{}。</P>
<P>3. 如果被调用的WebService方法没有返回值,应使用RPCServiceClient类的invokeRobust方法,该方法只有两个参数,它们的含义与invokeBlocking方法的前两个参数的含义相同。</P>
<P>4. 在创建QName对象时,QName类的构造方法的第一个参数表示WSDL文件的命名空间名,也就是&lt;wsdl:definitions&gt;元素的targetNamespace属性值,下面是SimpleService类生成的WSDL文件的代码片段:</P>
<UL>
<LI>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
<LI>&lt;wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://org.apache.axis2/xsd"
<LI>xmlns:ns="http://ws.apache.org/axis2" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl"
<LI>xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:xs="http://www.w3.org/2001/XMLSchema"
<LI>xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
<LI>xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
<LI>targetNamespace="http://ws.apache.org/axis2"&gt;
<LI>&nbsp; &nbsp; &lt;wsdl:types&gt;
<LI>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;
<LI>&nbsp; &nbsp; &lt;/wsdl:types&gt;
<LI>&nbsp; &nbsp;&nbsp;&nbsp;
<LI>&lt;/wsdl:definitions&gt; </LI></UL>
<P>复制代码</P>
<P><STRONG>四、用wsdl2java</STRONG><STRONG>简化客户端的编写</STRONG><BR>&nbsp; &nbsp;也许有很多读者会说“有没有搞错啊,只调用两个WebService方法用要写这么多代码,太麻烦了”。<BR>&nbsp; &nbsp;不过幸好Axis2提供了一个wsdl2java.bat命令可以根据WSDL文件自动产生调用WebService的代码。wsdl2java.bat命令可以在&lt;Axis2安装目录&gt;"bin目录中找到。在使用wsdl2java.bat命令之前需要设置AXIS2_HOME环境变量,该变量值是&lt;Axis2安装目录&gt;。<BR>&nbsp; &nbsp;在Windows控制台输出如下的命令行来生成调用WebService的代码:<BR>%AXIS2_HOME%\bin\wsdl2java -uri http://localhost:8080/axis2/services/SimpleService?wsdl-p client -s -o stub<BR>&nbsp; &nbsp;其中-url参数指定了wsdl文件的路径,可以是本地路径,也可以是网络路径。-p参数指定了生成的Java类的包名,-o参数指定了生成的一系列文件保存的根目录。在执行完上面的命令后,读者就会发现在当前目录下多了个stub目录,在."stub"src"client目录可以找到一个SimpleServiceStub.java文件,该文件复杂调用WebService,读者可以在程序中直接使用这个类,代码如下:</P>
<UL>
<LI>package client;
<LI>
<LI>import javax.xml.namespace.QName;
<LI>import org.apache.axis2.addressing.EndpointReference;
<LI>import org.apache.axis2.client.Options;
<LI>import org.apache.axis2.rpc.client.RPCServiceClient;
<LI>
<LI>public class StubClient
<LI>{
<LI>&nbsp; &nbsp; public static void main(String[] args) throws Exception&nbsp;&nbsp;
<LI>&nbsp; &nbsp; {
<LI>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;SimpleServiceStub stub = new SimpleServiceStub();
<LI>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;SimpleServiceStub.GetGreeting gg = new SimpleServiceStub.GetGreeting();
<LI>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;gg.setName("比尔");
<LI>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;System.out.println( stub.getGreeting(gg).get_return());
<LI>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;System.out.println(stub.getPrice().get_return());
<LI>&nbsp; &nbsp; }
<LI>} </LI></UL>
<P>复制代码</P>
<P>&nbsp; &nbsp;上面的代码大大简化了调用WebService的步骤,并使代码更加简洁。但要注意的是,wsdl2java.bat命令生成的Stub类将WebService方法的参数都封装在了相应的类中,类名为方法名,例如,getGreeting方法的参数都封装在了GetGreeting类中,要想调用getGreeting方法,必须先创建GetGreeting类的对象实例。<STRONG>五、使用C#</STRONG><STRONG>调用WebService</STRONG><BR>&nbsp; &nbsp;从理论上说,WebService可以被任何支持SOAP协议的语言调用。在Visual Studio中使用C#调用WebService是在所有语言中最容易实现的(VB.net的调用方法类似,也同样很简单)。<BR>&nbsp; &nbsp;新建一个Visual Studio工程,并在引用Web服务的对话框中输入如下的URL,并输入Web引用名为“WebService”:<BR>&nbsp; &nbsp; http:/ /localhost:8080/axis2/services/SimpleService?wsdl</P>
<P>&nbsp; &nbsp;然后引用Web服务的对话框就会显示该WebService中的所有的方法,如图5所示。</P>
<P style="TEXT-ALIGN: center" align=center sizset="9" sizcache="0">
<DIV class=tc><IMG style="WIDTH: 600px" alt=(1):用POJO实现0配置的WebService src="http://img.huomo.cn/article/f2d/78972/5.jpg"></DIV>
<P></P>
<P style="TEXT-ALIGN: center">图5</P>
<P>&nbsp; &nbsp; 在完成上面的工作后,只需要如下三行C#代码就可以调用getGreeting和getPrice方法,并显示这两个方法的返回值:</P>
<UL>
<LI>WebService.SimpleService simpleService = new WSC.WebService.SimpleService();
<LI>MessageBox.Show( simpleService.getGreeting("比尔"));
<LI>MessageBox.Show(simpleService.getPrice().@return.ToString()); </LI></UL>
<P>复制代码</P>
<P>&nbsp;&nbsp;在.net解析WSDL文件时直接将getGreeting方法的参数映射为String类型,因此,可以直接进行传值。<BR>&nbsp; &nbsp;从上面的调用过程可以看出,添加Web引用的过程就相当于在Java中调用wsdl2java.bat自动生成stub类的过程。只是在调用stub类时与C#有一定的区别,但从总体上来说,都大大简化了调用WebService的过程。</P></DIV>
<DIV class=adad style="PADDING-LEFT: 10px">/*500*200,创建于2010-10-22*/ var cpro_id = 'u250253';
<DIV style="DISPLAY: none">-</DIV></DIV></DIV>
页: [1]
查看完整版本: 用POJO实现0配置的WebService