首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

Eclipse环境下 使用Axis2 筹建WebService

2013-12-11 
Eclipse环境下 使用Axis2 搭建WebService??3.2、点击Services,显示的链接地址为,http://localhost:8080/axi

Eclipse环境下 使用Axis2 搭建WebService

?

?3.2、点击Services,显示的链接地址为,http://localhost:8080/axis2/services/listServices,这个页面显示内容为webservice项目发布的class文件,如果发布成功的话,则会在这里看到并且可以点击,这里以LogonService为例,LogonService为一个class文件,checkUserRight为LogonService提供的一个方法供客户端调用,如下图所示:


Eclipse环境下 使用Axis2 筹建WebService
?

?二、编写和发布WebService

?

1、用POJO形式发布(无需配置)

? (1)、在你的项目中,可以是web项目、也可以是JAVA项目,编写任何JAVA类,编译LogonService后将LogonService.class拷贝到<Tomcat安装目录>\webapps\axis2\WEB-INF\pojo目录中


Eclipse环境下 使用Axis2 筹建WebService
?
(2)、(如果没有pojo目录,则建立该目录),当然也可以修改此设置,进入到<Tomcat安装目录>\webapps\axis2\WEB-INF\conf? 打开axis2.xml,在102行如下图:


Eclipse环境下 使用Axis2 筹建WebService
?

?(3)、在浏览器地址栏中输入URL:http://localhost:8080/axis2/services/LogonService?wsdl,

得到如下界面说明成功。

<wsdl:definitions targetNamespace="http://ws.apache.org/axis2"><wsdl:types><xs:schema attributeFormDefault="qualified" elementFormDefault="unqualified" targetNamespace="http://ws.apache.org/axis2"><xs:element name="checkUserRight"><xs:complexType><xs:sequence><xs:element minOccurs="0" name="Name" nillable="true" type="xs:string"/></xs:sequence></xs:complexType></xs:element><xs:element name="checkUserRightResponse"><xs:complexType><xs:sequence><xs:element minOccurs="0" name="return" nillable="true" type="xs:string"/></xs:sequence></xs:complexType></xs:element></xs:schema></wsdl:types><wsdl:message name="checkUserRightRequest"><wsdl:part name="parameters" element="ns:checkUserRight"/></wsdl:message><wsdl:message name="checkUserRightResponse"><wsdl:part name="parameters" element="ns:checkUserRightResponse"/></wsdl:message><wsdl:portType name="LogonServicePortType"><wsdl:operation name="checkUserRight"><wsdl:input message="ns:checkUserRightRequest" wsaw:Action="urn:checkUserRight"/><wsdl:output message="ns:checkUserRightResponse" wsaw:Action="urn:checkUserRightResponse"/></wsdl:operation></wsdl:portType><wsdl:binding name="LogonServiceSoap11Binding" type="ns:LogonServicePortType"><soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/><wsdl:operation name="checkUserRight"><soap:operation soapAction="urn:checkUserRight" style="document"/><wsdl:input><soap:body use="literal"/></wsdl:input><wsdl:output><soap:body use="literal"/></wsdl:output></wsdl:operation></wsdl:binding><wsdl:binding name="LogonServiceSoap12Binding" type="ns:LogonServicePortType"><soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/><wsdl:operation name="checkUserRight"><soap12:operation soapAction="urn:checkUserRight" style="document"/><wsdl:input><soap12:body use="literal"/></wsdl:input><wsdl:output><soap12:body use="literal"/></wsdl:output></wsdl:operation></wsdl:binding><wsdl:binding name="LogonServiceHttpBinding" type="ns:LogonServicePortType"><http:binding verb="POST"/><wsdl:operation name="checkUserRight"><http:operation location="checkUserRight"/><wsdl:input><mime:content type="application/xml" part="parameters"/></wsdl:input><wsdl:output><mime:content type="application/xml" part="parameters"/></wsdl:output></wsdl:operation></wsdl:binding><wsdl:service name="LogonService"><wsdl:port name="LogonServiceHttpSoap11Endpoint" binding="ns:LogonServiceSoap11Binding"><soap:address location="http://localhost:8080/axis2/services/LogonService.LogonServiceHttpSoap11Endpoint/"/></wsdl:port><wsdl:port name="LogonServiceHttpSoap12Endpoint" binding="ns:LogonServiceSoap12Binding"><soap12:address location="http://localhost:8080/axis2/services/LogonService.LogonServiceHttpSoap12Endpoint/"/></wsdl:port><wsdl:port name="LogonServiceHttpEndpoint" binding="ns:LogonServiceHttpBinding"><http:address location="http://localhost:8080/axis2/services/LogonService.LogonServiceHttpEndpoint/"/></wsdl:port></wsdl:service></wsdl:definitions>

?

?

(4)、测试接口方法,输入:http://localhost:8080/axis2/services/LogonService/checkUserRight?Name=fengjunjie

其中checkUserRight是方法名,Name为参数名,一定注意区分大小写,并要和类中的方法参数全部对应。

?

?

<ns:checkUserRightResponse><return>fengjunjie</return></ns:checkUserRightResponse>

?注意:

?在编写、发布和测试WebService时应注意如下几点:
???? (1)、POJO类不能使用package关键字声明包,上面已经强调过。
???? (2)、 Axis2在默认情况下可以热发布WebService,也就是说,将WebService的.class文件复制到pojo目录中时Tomcat不需要重新启动就可以自动发布WebService, 如果想取消Axis2的热发布功能,可以打开<Tomcat安装目录>\webapps\axis2\WEB-INF\conf\axis2.xml,
??????? 找到如下的配置代码:

???????

<parameter name="hotdeployment">true</parameter>  

?? 将true改为false即可。要注意的是,Axis2在默认情况下虽然是热发布,但并不是热更新.
??也就是说,一旦成功发布了WebService,再想更新该WebService,就必须重启Tomcat。
??这对于开发人员调试WebService非常不方便,因此,在开发WebService时,可以将Axis2设为热更新。
??在axis2.xml文件中找到

<parameter name="hotupdate">false</parameter>

? 将false改为true即可。

?

? ? (3)、在浏览器中测试WebService时,如果WebService方法有参数,需要使用URL的请求参数来指定该WebService方法
???? 参数的值,请求参数名与方法参数名要一致,例如,要测试checkUserRight方法,请求参数名应为name,如上面的URL所示。

??? (4)、 发布WebService的pojo目录只是默认的,如果读者想在其他的目录发布WebService,
???? 可以打开axis2.xml文件,并在<axisconfig>元素中添加如下的子元素:

<deployer extension=".class" directory="my" src="/img/2013/12/11/145001114.png">
?
?(2)、新建一个单独的一个文件夹文件夹名任意,在该目录下新建一个META-INF,在META-INF下新建一个services.xml,下,根据自己机器实际路径来放。

?

<?xml version="1.0" encoding="UTF-8"?>  <service name="WithPageLogonService">      <description>          Web Service例子      </description>      <parameter name="ServiceClass">          com.sinosoft.webservice.HelloServiceNew      </parameter>      <messageReceivers>          <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"              />          <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"              />      </messageReceivers>  </service> 
?然后使用cmd命令进入打包目录就是刚才新建那个目录,运行?? jar cvf ws.aar . 完成打包,实际上,.jar文件? 也可以发布webservice,但axis2官方文档中建议使用.aar文件发布webservice.
??最后将ws.aar文件复制到<Tomcat安装目录>\webapps\axis2\WEB-INF\services目录中,
??启动Tomcat后,就可以调用这个WebService了。

?

?

另外services.xml文件中也可以直接指定WebService类的方法,如可以用下面的配置代码来发布WebService

?

Xml代码:

?

    <service name=" WithPageLogonService">      <description>          Web Service例子      </description>      <parameter name="ServiceClass">          com.sinosoft.webservice.HelloServiceNew        </parameter>      <operation name="checkUserRight">          <messageReceiver name="code">    <serviceGroup>      <service name="myService1">          ...      </service>      <service name="myService2">          ...      </service>      </serviceGroup>  
?三、 用Java实现调用WebService的客户端程序

?

import javax.xml.namespace.QName;import org.apache.axis2.AxisFault;import org.apache.axis2.addressing.EndpointReference;import org.apache.axis2.client.Options;import org.apache.axis2.rpc.client.RPCServiceClient;/** * * @ClassName: TestClient * @Description: WebService 服务测试类 * @author 冯俊杰 * @date 2013-12-6 下午4:03:38 * */public class TestClient {private static String URL = "http://localhost:8080/axis2/services/LogonService?wsdl";public static void main(String[] args) throws AxisFault {// 使用RPC方式调用WebServiceRPCServiceClient serviceClient = new RPCServiceClient();Options options = serviceClient.getOptions();// 指定调用WebService的URLEndpointReference targetEPR = new EndpointReference(URL);options.setTo(targetEPR);// 指定sayHelloToPerson方法的参数值Object[] opAddEntryArgs = new Object[] { "冯俊杰" };// 指定sayHelloToPerson方法返回值的数据类型的Class对象@SuppressWarnings("rawtypes")Class[] classes = new Class[] { String.class };// 指定要调用的checkUserRight方法及WSDL文件的命名空间QName opAddEntry = new QName("http://ws.apache.org/axis2","checkUserRight");// 调用checkUserRight方法并输出该方法的返回值System.out.println(serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0]);}}
? 输出结果为:
?? 冯俊杰在编写客户端代码时应注意如下几点:

???? 1. 客户端代码需要引用很多Axis2的jar包,如果读者不太清楚要引用哪个jar包,
??????? 可以在Eclipse的工程中引用Axis2发行包的lib目录中的所有jar包。


???? 2. 在本例中使用了RPCServiceClient类的invokeBlocking方法调用了WebService中的方法。
?????? invokeBlocking方法有三个参数:

????? (1)、其中第一个参数的类型是QName对象,表示要调用的方法名;
????? (2)、 第二个参数表示要调用的WebService方法的参数值,参数类型为Object[];
?????? (3)、第三个参数表示WebService方法的返回值类型的Class对象,参数类型为Class[]。
?????? 当方法没有参数时,invokeBlocking方法的第二个参数值不能是null,而要使用new Object[]{}。


???? 3. 如果被调用的WebService方法没有返回值,应使用RPCServiceClient类的invokeRobust方法,
??????? 该方法只有两个参数,它们的含义与invokeBlocking方法的前两个参数的含义相同。


???? 4. 在创建QName对象时,QName类的构造方法的第一个参数表示WSDL文件的命名空间名,
???? ?也就是<wsdl:definitions>元素的targetNamespace属性值。

2、wsdl2java.bat命令方式

?Axis2提供了一个wsdl2java.bat命令可以根据WSDL文件自动产生调用WebService的代码。
??wsdl2java.bat命令可以在<Axis2安装目录>/bin目录中找到。
??在使用wsdl2java.bat命令之前需要设置AXIS2_HOME环境变量,该变量值是<Axis2安装目录>。
??在Windows控制台输出如下的命令行来生成调用WebService的代码:
??%AXIS2_HOME%\bin\wsdl2java -uri http://localhost:8080/axis2/services/WithPageLogonService?wsdl
?????????-p client -s -o stub
??其中-url参数指定了wsdl文件的路径,可以是本地路径,也可以是网络路径。
??-p参数指定了生成的Java类的包名,-o参数指定了生成的一系列文件保存的根目录。
??在执行完上面的命令后,就会发现在当前目录下多了个stub目录,
??在stub/src/client目录可以找到一个HelloServiceStub.java文件,
??该文件复杂调用WebService,可以在程序中直接使用这个类,代码如下:

?

? 上面的代码大大简化了调用WebService的步骤,并使代码更加简洁。
??但要注意的是,wsdl2java.bat命令生成的Stub类将WebService方法的参数都封装在了相应的类中,
??类名为方法名,例如,sayHelloToPerson方法的参数都封装在了SayHelloToPerson类中,
??要想调用sayHelloToPerson方法,必须先创建SayHelloToPerson类的对象实例。

热点排行