Axis实践之Axis入门

来源:互联网 发布:手机怎么进电脑版淘宝 编辑:程序博客网 时间:2024/04/28 20:19

第一部分: 安装Axis

第一步: 下载Axis
从Apache网站下载Axis发布包:
http://ws.apache.org/axis/

我下载的是
Axis 1.4 Final 版


第二步: 解压Axis压缩包
把下载的axis-bin-1_4.zip包解压缩到一个文件夹,可以是任意的,但如果文件夹包含中文名时,在中间有些过程需要对中文进行一些处理,否则可能出错;
我把axis-bin-1_4.zip解压到 F:/AXIS_Study,解压后的目录结构是这样的:
F:/AXIS_Study
    |
    |____axis-bin-1_4
    |
    |-----docs (文件夹)
    |
    |-----lib (文件夹)
    |
    |-----samples (文件夹)
    |
    |-----webapps (文件夹)
    |
    |-----xmls (文件夹)
    |
    |-----LICENSE
    |
    |-----NOTICES
    |
    |-----README
    |
    |-----release-notes.html

第三步:准备Web Server
准备一个Web Server,基本上所有主流的Web Server都可以,为了节省资源,我使用Tomcat5.5.23来讲解这个例子,大家可以自行到Apache网站上下载该版本的Tomcat。


第四步: 配置Tomcat
进入Tomcat目录,在conf/Catalina/localhost目录下新建一个XML文件,命名为axis.xml,然后在文件中输入如下内容:

Xml代码 复制代码
  1. <?xml version="1.0" encoding="gb2312"?>    
  2. <!--   
  3. Axis学习工程   
  4. -->    
  5. <Context docBase="F:/AXIS_Study/axis-bin-1_4/webapps/axis" >    
  6. </Context>   

 
这个文件是用来在Tomcat中配置一个Web应用的,这不在本次课程的范围,大家可以自行找相关资料来看。注意在文件中
docBase="F:/AXIS_Study/axis-bin-1_4/webapps/axis"
是指向Axis解压目录下面的webapps/axis目录,大家根据自己的目录结构自行修改


第五步:启动Tomcat
进入到Tomcat安装目录下的bin目录,找到startup.bat并执行之,在启动过程中,控制台窗口应该会出现以下信息:
2008-5-15 14:45:55 org.apache.catalina.core.AprLifecycleListener lifecycleEvent
信息: The Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: D:/JAVATools/jdk1.5.0
_05/bin;.;C:/windows/system32;C:/windows;D:/tools/系统/UnxUtils/bin;D:/tools/系统/UnxUtils/usr/local/wbin;D:/JAVATools/jdk1.5.0_05/bin;C:/oracle92/bin;C:/Progra
m Files/Oracle/jre/1.3.1/bin;C:/Program Files/Oracle/jre/1.1.8/bin;C:/windows/system32;C:/windows;C:/windows/System32/Wbem;D:/tools/系统;D:/JAVATools/ant-1.6.2/
bin
2008-5-15 14:45:55 org.apache.coyote.http11.Http11BaseProtocol init
信息: Initializing Coyote HTTP/1.1 on http-80
2008-5-15 14:45:55 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 859 ms
2008-5-15 14:45:55 org.apache.catalina.core.StandardService start
信息: Starting service Catalina
2008-5-15 14:45:55 org.apache.catalina.core.StandardEngine start
信息: Starting Servlet Engine: Apache Tomcat/5.5.23
2008-5-15 14:45:55 org.apache.catalina.core.StandardHost start
信息: XML validation disabled
- Unable to find config file. Creating new servlet engine config file: /WEB-INF/server-config.wsdd
- Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled.

……

注意,最后面三行指示了Axis 的web app已经启动了,因为在我的Tomcat中发布的应用中,没有其他应用有使用Web Service,现在出现 .wsdd 文件,肯定就是Axis的应用启动了。
我们到
F:/AXIS_Study/axis-bin-1_4/webapps/axis/WEB-INF
目录下查看,发现系统自动生成了一个attachments目录,这与启动日志中的Attachment support is disabled.是对应的。



第六步:查看一下当前Axis中布署了哪些Web Service
打开浏览器,在地址栏输入:
http://localhost/axis/servlet/AxisServlet

浏览器显示结果:
And now... Some Services

  AdminService (wsdl)
      AdminService
  Version (wsdl)
     getVersion
这说明在Axis中已经布署了两个WebService,只不过这两个WebService都是Axis自带的,第一个是管理用的WebService,第二个是查看当前Axis版本信息的WebService。
大家可以点击相应的链接“wsdl”来看一下这两个WebService的 wsdl 文件。


结语:
至此,我们已经成功地把Axis 1.4 安装到Tomcat Web 服务器上了,接下来的目标是使用Axis来发布Web Service。



第二部分: 使用Axis发布Web Service
Axis提供两种方式将Java类发布成Web Services,这两种方式分别是:即时快速自动发布 和 通过配置文件进行发布,下面我们按顺序来讲如何使用这两种方式来发布WebService

即时快速自动发布:
第一步: 编写业务类
为了发布一个Web Service,首先必须要有业务类,为了节约时间,我写一个最简单的业务类:

Java代码 复制代码
  1. public class SayHello{    
  2.   private String name;    
  3.   public String hello(){    
  4.     return "Hello, axis Ver1.4 talking to you.";    
  5.   }    
  6. }   

 
注意,采用快速自动发布的Web Service的业务类不能带 package,也不能实现任何接口(Interface),因为Axis自动发布时不支持Package。


第二步:把SayHello发布为Web Service
把SayHello.java复制到 axis目录下的webapps/axis目录下,然后把文件扩展名改为:jws


第三步:访问刚发布的Web Service
打开浏览器,在地址栏里输入:
http://localhost/axis/SayHello.jws

浏览器显示结果如下:
There is a Web Service here

Click to see the WSDL

这表示Web Service已经发布成功了。我们可以点击连接“Click to see the WSDL ”来看看生成的wsdl文件:

Xml代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <wsdl:definitions targetNamespace="http://localhost/axis/SayHello.jws" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://localhost/axis/SayHello.jws" xmlns:intf="http://localhost/axis/SayHello.jws" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">    
  3. <!--WSDL created by Apache Axis version: 1.4    
  4. Built on Apr 22, 2006 (06:55:48 PDT)-->    
  5.   <wsdl:message name="helloResponse">    
  6.     <wsdl:part name="helloReturn" type="xsd:string"/>    
  7.   </wsdl:message>    
  8.   <wsdl:message name="helloRequest">    
  9.   </wsdl:message>    
  10.   <wsdl:portType name="SayHello">    
  11.     <wsdl:operation name="hello">    
  12.       <wsdl:input message="impl:helloRequest" name="helloRequest"/>    
  13.       <wsdl:output message="impl:helloResponse" name="helloResponse"/>    
  14.     </wsdl:operation>    
  15.   </wsdl:portType>    
  16.   
  17. <wsdl:binding name="SayHelloSoapBinding" type="impl:SayHello">    
  18.   <wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>    
  19.   <wsdl:operation name="hello">    
  20.     <wsdlsoap:operation soapAction=""/>    
  21.     <wsdl:input name="helloRequest">    
  22.         <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://DefaultNamespace" use="encoded"/>    
  23.     </wsdl:input>    
  24.     <wsdl:output name="helloResponse">    
  25.       <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost/axis/SayHello.jws" use="encoded"/>    
  26.     </wsdl:output>    
  27.   </wsdl:operation>    
  28. </wsdl:binding>    
  29.   
  30.   <wsdl:service name="SayHelloService">    
  31.     <wsdl:port binding="impl:SayHelloSoapBinding" name="SayHello">    
  32.       <wsdlsoap:address location="http://localhost/axis/SayHello.jws"/>    
  33.     </wsdl:port>    
  34.   </wsdl:service>    
  35. </wsdl:definitions>   

 这个文件结构跟我们以前用IDE生成的没什么两样。

我们再访问一下这个Web Service,看看结果对不对,在浏览器地址栏输入:
http://localhost/axis/SayHello.jws?method=hello

浏览器显示服务器返回的结果:

Xml代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8" ?>    
  2. <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">    
  3.   <soapenv:Body>    
  4.     <helloResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">    
  5.       <helloReturn xsi:type="xsd:string">Hello, axis Ver1.4 talking to you.</helloReturn>    
  6.     </helloResponse>    
  7.   </soapenv:Body>    
  8. </soapenv:Envelope>   

 这里返回的是整个SOAP数据包

接头我们用Jbuilder来生成该Web Service的客户端代码:
打开Jbuilder,新建一个工程,然后增加Axis Web Service Client支持:
菜单: New --> Web Service(J2EE1.3) --> Axis Web Service Client Configuration
然后在Web Service设计器中选择“Import from URL”,其中的wsdl文件路径就输入“http://localhost/axis/SayHello.jws?wsdl”
Jbuilder就自动帮我们生成了所有配置。然后再make这个web service,这时候JBuilder自动生成了所需要的全部代码,还包括一个JUnit测试类 SayHelloServiceTestCase,为了能更直观地看到结果,我们再写一个自己的测试类:

Java代码 复制代码
  1. public class SayHelloTest {    
  2.   public void test(){    
  3.     SayHelloSoapBindingStub binding;    
  4.     try {    
  5.       binding = (SayHelloSoapBindingStub) new SayHelloServiceLocator().getSayHello(); //取接口    
  6.   
  7.     binding.setTimeout(60000); //设置超时时间    
  8.   
  9.     String helloMsg = binding.hello(); //调用hello方法    
  10.     
  11.     System.out.println(">>>>>>>>>>>>>>>>服务器返回消息:");    
  12.       System.out.println( helloMsg );    
  13.       System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");    
  14.     }catch (javax.xml.rpc.ServiceException ex) {    
  15.       if (ex.getLinkedCause() != null)    
  16.         ex.getLinkedCause().printStackTrace();    
  17.       System.out.println("JAX-RPC调用出错:");    
  18.       ex.printStackTrace();    
  19.     } catch (RemoteException ex) {      
  20.       System.out.println("Web Service调用出错:");    
  21.       ex.printStackTrace();    
  22.   }    
  23.   
  24.   }    
  25.      
  26.   public static void main(String[] args) {    
  27.     SayHelloTest sayhellotest = new SayHelloTest();    
  28.   
  29.     sayhellotest.test();    
  30.   }    
  31. }    

 
编译,然后执行该类,得到输出结果是:
>>>>>>>>>>>>>>>>服务器返回消息:
Hello, axis Ver1.4 talking to you.
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

至此,我们通过Axis自动发布的Web Service成功了。


=======================
通过配置文件(WSDD)进行发布:

第一步:先写一个配置文件 server-config.wsdd

Xml代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <deployment name="defaultClientConfig"    
  3. xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"    
  4. xmlns:handler="http://xml.apache.org/axis/wsdd/providers/handler" xmlns="http://xml.apache.org/axis/wsdd/">    
  5.   <globalConfiguration name="defaultClientConfig">    
  6.     <requestFlow name="RequestFlow1" type="">    
  7.         <handler name="Handler1" type="java:org.apache.axis.handlers.JWSHandler">    
  8.           <parameter name="scope" value="session"/>    
  9.         </handler>    
  10.         <handler name="Handler2" type="java:org.apache.axis.handlers.JWSHandler">    
  11.             <parameter name="scope" value="request"/>    
  12.             <parameter name="extension" value=".jwr"/>    
  13.         </handler>    
  14.       </requestFlow>    
  15.     </globalConfiguration>    
  16.     <handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/>    
  17.     <handler name="LocalResponder" type="java:org.apache.axis.transport.local.LocalResponder"/>    
  18.     <handler name="Authenticate" type="java:org.apache.axis.handlers.SimpleAuthenticationHandler"/>    
  19.     <transport name="http" type="">    
  20.         <requestFlow name="RequestFlow1" type="">    
  21.         <handler name="Handler1" type="URLMapper"/>    
  22.         <handler name="Handler2" type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/>    
  23.       </requestFlow>    
  24.     </transport>    
  25.     <transport name="local" type="">    
  26.         <responseFlow name="ResponseFlow1" type="">    
  27.             <handler name="Handler1" type="LocalResponder"/>    
  28.         </responseFlow>    
  29.     </transport>    
  30.   
  31.   <!--这里配置了一个Web Service,如果有多个Web Service,就按这种格式在下面增加即可-->    
  32.   <service name="SayHello2" provider="java:RPC">    
  33.     <parameter name="scope" value="Request"/>    
  34.     <parameter name="className" value="SayHello"/>    
  35.     <parameter name="allowedMethods" value="*"/>    
  36.   </service>    
  37.   
  38. </deployment>   

 
需要注意的通过配置文件来发布Web Service时,业务类是可以有package的,这里为了利用上面的业务类,所以就没有package

然后把该配置文件放置到WEB-INF目录下。

第二步:编译业务类
把上面写的业务编译成class文件,放到WEB-INF/classes目录下

第三步:重启Tomcat

第四步: 查看新发布的Web Service
打开浏览器,地址栏里输入: http://localhost/axis/servlet/AxisServlet
浏览器返回的结果如下:
And now... Some Services
SayHello2 (wsdl)
hello

这表明我们的新Web Service发布成功了,我们可以进一步看一下wsdl文件:

Xml代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <wsdl:definitions targetNamespace="http://localhost/axis/services/SayHello2" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://localhost/axis/services/SayHello2" xmlns:intf="http://localhost/axis/services/SayHello2" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">    
  3. <!--WSDL created by Apache Axis version: 1.4    
  4. Built on Apr 22, 2006 (06:55:48 PDT)    
  5. -->    
  6. <wsdl:message name="helloRequest">    
  7. </wsdl:message>    
  8. <wsdl:message name="helloResponse">    
  9. <wsdl:part name="helloReturn" type="soapenc:string"/>    
  10. </wsdl:message>    
  11. <wsdl:portType name="SayHello">    
  12. <wsdl:operation name="hello">    
  13. <wsdl:input message="impl:helloRequest" name="helloRequest"/>    
  14. <wsdl:output message="impl:helloResponse" name="helloResponse"/>    
  15. </wsdl:operation>    
  16. </wsdl:portType>    
  17. <wsdl:binding name="SayHello2SoapBinding" type="impl:SayHello">    
  18. <wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>    
  19. <wsdl:operation name="hello">    
  20. <wsdlsoap:operation soapAction=""/>    
  21. <wsdl:input name="helloRequest">    
  22. <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://DefaultNamespace" use="encoded"/>    
  23. </wsdl:input>    
  24. <wsdl:output name="helloResponse">    
  25. <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost/axis/services/SayHello2" use="encoded"/>    
  26. </wsdl:output>    
  27. </wsdl:operation>    
  28. </wsdl:binding>    
  29.   
  30. <wsdl:service name="SayHelloService">    
  31. <wsdl:port binding="impl:SayHello2SoapBinding" name="SayHello2">    
  32. <wsdlsoap:address location="http://localhost/axis/services/SayHello2"/>    
  33. </wsdl:port>    
  34. </wsdl:service>    
  35. </wsdl:definitions>   

 
第五步:写测试客户端
在Jbuilder中,刚才的工程里增加一个Web Service客户端,还是在Web Service设计器中选择“Import from URL”,其中的wsdl文件路径就输入“http://localhost/axis/services/SayHello2?wsdl”,然后make这个web service module。同样的,我们也再写一个自己的测试类:

Java代码 复制代码
  1. public class SayHello2Test {    
  2.     public void test(){    
  3.         SayHello2SoapBindingStub binding;    
  4.         try {    
  5.             binding = (SayHello2SoapBindingStub) new SayHello2ServiceLocator().getSayHello(); //取接口    
  6.   
  7.             binding.setTimeout(60000); //设置超时时间    
  8.   
  9.             String helloMsg = binding.hello(); //调用hello方法    
  10.   
  11.             System.out.println(">>>>>>>>>>>>>>>>服务器返回消息:");    
  12.             System.out.println( helloMsg );    
  13.             System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");    
  14.         }catch (javax.xml.rpc.ServiceException ex) {    
  15.             if (ex.getLinkedCause() != null)    
  16.                 ex.getLinkedCause().printStackTrace();    
  17.             System.out.println("JAX-RPC调用出错:");    
  18.             ex.printStackTrace();    
  19.         } catch (RemoteException ex) {    
  20.             System.out.println("Web Service调用出错:");    
  21.             ex.printStackTrace();    
  22.         }    
  23.   
  24.     }    
  25.   
  26.     public static void main(String[] args) {    
  27.         SayHello2Test sayhellotest = new SayHello2Test();    
  28.   
  29.         sayhellotest.test();    
  30.     }    
  31. }   

 
运行这个测试类,得到的输出结果是:
>>>>>>>>>>>>>>>>服务器返回消息:
你好,现在是 axis Ver1.4 在与你对话。
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

OK, 一切尽在掌握中,哈哈!


好了,今天课程到此结束,下次再见。

原创粉丝点击