2007-08-11
Axis客户端调用实例
关键字: axis
最近一个项目中需要用到调用WebService,实现相关功能,由于一直都是用Xfire来实现WebSerivce的应用,所以这次调用客户用Axis生成的webService时,也选用Axis来生成客户端实现调用。应用的过程也是学习的过程,下面列出具体的例子,以大家参考,互相学习。
一 .环境配置
?首先下载Axis1.x和JDK、TOMCAT。
Axis1.x : http://ws.apache.org/axis/ 我下载的是axis-bin-1_4
JDK1.5和Tomcat5大家可以参考其它安装配置说明
?然后解压Axis1.4,然后将axis-1_4\webapps目录下的axis工程拷贝到TOMCAT的webapps目录下
?启动TOMCAT服务器,打开localhost:8080/axis/ 如果正确显示,如果显示正确,可以看到如下:
Hello! Welcome to Apache-Axis.
What do you want to do today?
?Validation - Validate the local installation's configuration
see below if this does not work.
?List - View the list of deployed Web services
?Call - Call a local endpoint that list's the caller's http headers (or see its WSDL).
?Visit - Visit the Apache-Axis Home Page
?Administer Axis - [disabled by default for security reasons]
?SOAPMonitor - [disabled by default for security reasons]
To enable the disabled features, uncomment the appropriate declarations in WEB-INF/web.xml in the webapplication and restart it.
Validating Axis
If the "happyaxis" validation page displays an exception instead of a status page, the likely cause is that you have multiple XML parsers in your classpath. Clean up your classpath by eliminating extraneous parsers.
If you have problems getting Axis to work, consult the Axis Wiki and then try the Axis user mailing list.
?点击Validation可以查询需要的JAR是不是都存在,如果不存大,可以根据URL进行下载,下载后拷贝到Tomcat的webapps\axis\WEB-INF\lib目录下。如果JAR都存在了,则显示如下信息:
Needed Components
?Found SAAJ API ( javax.xml.soap.SOAPMessage ) at D:\dev\apache-tomcat-6.0.14\webapps\axis\WEB-INF\lib\saaj.jar
?Found JAX-RPC API ( javax.xml.rpc.Service ) at D:\dev\apache-tomcat-6.0.14\webapps\axis\WEB-INF\lib\jaxrpc.jar
?Found Apache-Axis ( org.apache.axis.transport.http.AxisServlet ) at D:\dev\apache-tomcat-6.0.14\webapps\axis\WEB-INF\lib\axis.jar
?Found Jakarta-Commons Discovery ( org.apache.commons.discovery.Resource ) at D:\dev\apache-tomcat-6.0.14\webapps\axis\WEB-INF\lib\commons-discovery-0.2.jar
?Found Jakarta-Commons Logging ( org.apache.commons.logging.Log ) at D:\dev\apache-tomcat-6.0.14\webapps\axis\WEB-INF\lib\commons-logging-1.0.4.jar
?Found Log4j ( org.apache.log4j.Layout ) at D:\dev\apache-tomcat-6.0.14\webapps\axis\WEB-INF\lib\log4j-1.2.8.jar
?Found IBM's WSDL4Java ( com.ibm.wsdl.factory.WSDLFactoryImpl ) at D:\dev\apache-tomcat-6.0.14\webapps\axis\WEB-INF\lib\wsdl4j-1.5.1.jar
?Found JAXP implementation ( javax.xml.parsers.SAXParserFactory ) at an unknown location
?Found Activation API ( javax.activation.DataHandler ) at D:\dev\apache-tomcat-6.0.14\webapps\axis\WEB-INF\lib\activation.jar
Optional Components
?Found Mail API ( javax.mail.internet.MimeMessage ) at D:\dev\apache-tomcat-6.0.14\webapps\axis\WEB-INF\lib\mail.jar
?Found XML Security API ( org.apache.xml.security.Init ) at D:\dev\apache-tomcat-6.0.14\webapps\axis\WEB-INF\lib\xmlsec-1.4.1.jar
?Found Java Secure Socket Extension ( javax.net.ssl.SSLSocketFactory ) at an unknown location
?到这步就可以进行Axis的服务端发布WebService了,Web Service服务端开发更多具体方法请参考其它文档。下面是一种介绍
编写JAVA类Hello.java,内容如下:
java 代码
1.public class Hello{
2. public String hello(String name){
3. if(name==null)
4. name = "";
5. return "你好"+name+",欢迎来到Web服务的世界!";
6.}
7.}
8.
仅此而已,无需编译,将该文件改名为Hello.jws并拷贝到AXIS应用目录Tomcat 的 webapps\axis下。
下面我们就可以测试该Web服务了,打开浏览器并输入刚刚创建的文件名对应的URL地址 http://localhost:8080/axis/Hello.jws 浏览器显示如下结果:
There is a Web Service here
Click to see the WSDL
点击可以查看到WSDL文档,如下
xml 代码
1.<?xml version="1.0" encoding="UTF-8" ?>
2.-<wsdl:definitions
3. targetNamespace="http://localhost:8080/axis/Hello.jws"
4. xmlns="http://schemas.xmlsoap.org/wsdl/"
5. xmlns="http://www.w3.org/2000/xmlns/"
6. xmlns:apachesoap="http://xml.apache.org/xml-soap"
7. xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
8. xmlns:impl="http://localhost:8080/axis/Hello.jws"
9. xmlns:intf="http://localhost:8080/axis/Hello.jws"
10. xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"
11. xmlns:xsd="http://www.w3.org/2001/XMLSchema"
12. xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
13. - <wsdl:message name="helloRequest">
14. <wsdl:part name="name" type="xsd:string" />
15. </wsdl:message>
16. + <wsdl:message name="helloResponse">
17. - <wsdl:portType name="Hello">
18. - <wsdl:operation name="hello" parameterOrder="name">
19. <wsdl:input name="helloRequest" message="intf:helloRequest" />
20. <wsdl:output name="helloResponse" message="intf:helloResponse" />
21. </wsdl:operation>
22. </wsdl:portType>
23. - <wsdl:binding name="HelloSoapBinding" type="intf:Hello">
24. <wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
25. - <wsdl:operation name="hello">
26. <wsdlsoap:operation soapAction="" />
27. - <wsdl:input name="helloRequest">
28. <wsdlsoap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
29. namespace="http://DefaultNamespace" />
30. </wsdl:input>- <wsdl:output name="helloResponse">
31. <wsdlsoap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
32. namespace="http://localhost:8080/axis/Hello.jws" />
33. </wsdl:output>
34. </wsdl:operation>
35. </wsdl:binding>
36. - <wsdl:service name="HelloService">
37. - <wsdl:port name="Hello" binding="intf:HelloSoapBinding">
38. <wsdlsoap:address location="http://localhost:8080/axis/Hello.jws" />
39. </wsdl:port>
40. </wsdl:service>
41. </wsdl:definitions>
42.
?得到WSDL文档后,现在进行AXIS客户端的生成。我们前面安装的AXIS环境中已经包含着这样的工具,它是一个JAVA类,类名为:org.apache.axis.wsdl.WSDL2Java。打开命令行窗口,转到刚才TOMCAT的 webapps\axis 目录下的WEB-INF子目录。确保Tomcat服务已经处于启动状态,键入命令 :
Java -Djava.ext.dirs=lib org.apache.axis.wsdl.WSDL2Java http://localhost:8080/axis/services/Hello?WSDL //这个地方可以是WSDL文件,也可以是WSDL的路径地址
?该命令执行的结果是在当前所在目录(TOMCAT的 webapps\axis WEB-INF子目录)下产生一个子目录 ,该目录下有四个JAVA源文件,它们分别是:
Hello.java 定义了Web服务接口,此例中只有一个hello方法。
HelloService.java 定义了用于获取Web服务接口的方法。
HelloServiceLocator.java 接口HelloService的具体实现。
HelloSoapBindingStub.java Web服务客户端桩,通过该类与服务器交互。
?这四个JAVA类帮我们处理了大部分的逻辑,我们需要的仅仅是把这些类加到我们的项目然后创建一个我们自己的类来调用它们即可。为此我们新加一个类Main.java,为了方便,让这个类与刚产生的四个类都在同一个包下。内容如下:
java 代码
1.//Main.java
2.package localhost.axis.Hello_jws;
1.public class Main{
1.public static void main(String[] args) throws Exception{
1. HelloService service = new HelloServiceLocator();
2. Hello hello = service.getHello();
3. System.out.println("Response:"+hello.hello("罐头"));
4. }
5.}
6.
在Eclipse或把Java文件编译后执行,即可调用Axis的WebService。实现具体功能。
以上重点在于介绍Axis的客户端调用,对于初学者应该注意Axis环境的设置,和用WSDL2Java生成客户端文件。由于Axis1.x没有什么Eclipse插件可以自动化完成客户端的生成,所以这样有点繁琐。希望对大家有所帮助。