axis2开发并调用webservice

本文将介绍如何使用 Tomcat5.0和 Apache Axis2开发、部署及测试一个简单的 Web Service应用。
1. 工作环境
Eclipse 3.1.2+Lomboz+jdk1.5+ apache-tomcat-5.0.18+AXIS2:1.0(war版本和 bin版本 )
在 [url]http://ws.apache.org/axis2/download/1_0/download.cgi[/url]页面 下 , 下载 AXIS2的 Binary Distribution url: [url]http://apache.justdn.org/ws/axis2/1_0/axis2-std-1.0-bin.zip[/url]和 war Distribution url: [url]http://apache.justdn.org/ws/axis2/1_0/axis2-1.0-docs.zip[/url]。把这两个文件解压,比如解压缩的后得目录为 C:"axis2-std-1.0-bin和 C:"axis2.war。
在 Eclipse下通过菜单 window—preferences…--Java—Build Path—User Libraries 新建一个 user library,比如名字就叫 axis2把 C:"axis2-std-1.0-bin"lib下的所有 jar文件包含进来。把 axis2.war拷贝到 %TOMCAT-HOME%/webapps下面。
 
2.检验安装
在 Eclipse下启动 Tomcat,在地址栏内输入 [url]http://localhost:8080/axis2/[/url]
点击 Validate,将到达  Axis2 Happiness Page。
3. WebService中的 HelloWorld
1)新建一个动态 web工程,取名 ZZaxis,右键点击项目名,选择 Properties-Java Build Path-Add Library-User Library-axis2。
 
2)新建 package sample,建立 HelloWorld.java,代码如下。
HelloWorld.java
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
 
public class HelloWorld {
       public OMElement sayHello(OMElement in){
              String name=in.getText();
              String info=name+"HelloWorld!";
              OMFactory fac=OMAbstractFactory.getOMFactory();
              OMNamespace omNs=fac.createOMNamespace("http://helloworld.com/","hw");
              OMElement resp=fac.createOMElement("sayHelloResponse",omNs);
              resp.setText(info);
              return resp;
       }
}
 
3)在 WebContent"META-INF"建立 services.xml,代码如下。
services.xml
<?xml version="1.0" encoding="UTF-8"?>
<service name="HelloWorld">
<description>
 This is a sample Web Service.
</description>
<parameter name="ServiceClass" locked="false">sample.HelloWorld</parameter>
<operation name="sayHello">
 <messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
</operation>
</service>
 
4)将目录 sample和目录 META-INF组织如下(新建目录 example)。
+-example
|-------- +-sample
    |------- HelloWorld.class
|---------+-META-INF
       |-------services.xml
 
5)打包生成 aar文件。
在命令符环境下,将当前目录转到 example。
jar cvf HelloWorld.aar . //注意最后一个点,在当前目录下生成 HelloWorld.aar。
 
6)在 Eclipse中启动 Tomcat,在地址栏下键入 [url]http://localhost:8080/axis2/[/url]。选择 Administration,输入用户名 admin,密码 axis2。选择左侧工具栏 Tools- Upload Service,上传之前打包的 HelloWorld.aar。该文件将在 <CATALINA_HOME>/webapps/axis2"WEB-INF"services目录下。
 
7)编写客户端检验代码。新建 Java Project,取名为 ZZaxisClient。右键点击项目名,选择 Properties-Java Build Path-Add Library-User Library-axis2。
 
8)新建 package example.client。建立 TestClient.java,代码如下。
TestClient.java
package example.client;
 
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
 
public class TestClient {
       private static EndpointReference targetEPR=new EndpointReference
        ("http://localhost:8080/axis2/services/HelloWorld");
       public static OMElement getSayHelloOMElement(){
              OMFactory fac=OMAbstractFactory.getOMFactory();
              OMNamespace omNs=fac.createOMNamespace("http://helloworld.com/","hw");
              OMElement method=fac.createOMElement("sayHello",omNs);
              method.setText("ZJ");
              return method;
       }
       public static void main(String[] args){
              try{
                     Options options=new Options();
                     options.setTo(targetEPR);
                     ServiceClient sender=new ServiceClient();
                     sender.setOptions(options);
                     OMElement sayHello=TestClient.getSayHelloOMElement();
                     OMElement result=sender.sendReceive(sayHello);
                     System.out.println(result);
              }
              catch(Exception axisFault){
                     axisFault.printStackTrace();
              }
       }
}
 
9)测试, run TestClient.java as Java Application。结果:
<hw:sayHelloResponse xmlns:hw="http://helloworld.com/"
xmlns:tns="http://ws.apache.org/axis2">
ZJHelloWorld!

</hw:sayHelloResponse>


1. HelloWorld做了些什么?
HelloWorld功能非常简单,在客户端输入你的姓名,本例中为 ZJ。参数传递到服务器端后,经过处理将返回 name+"HelloWorld!",本例中为 ZJ HelloWorld!
 
2.服务器端文件 HelloWorld.java
HelloWorld.java
package sample;
 
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
 
public class HelloWorld {
    //读取 client端 getSayHelloOMElement()方法传递的参数 in。
       public OMElement sayHello(OMElement in){
        //将 in转换为 String。
              String name=in.getText();
              String info=name+"HelloWorld!";
        //创建 response SOAP包。
              OMFactory fac=OMAbstractFactory.getOMFactory();
        // OMNamespace指定此 SOAP文档名称空间。
              OMNamespace omNs=fac.createOMNamespace("http://helloworld.com/","hw");
        //创建元素 sayHello,并指定其在 omNs指代的名称空间中。
              OMElement resp=fac.createOMElement("sayHelloResponse",omNs);
        //指定元素的文本内容。
              resp.setText(info);
              return resp;
       }
}
 
3. services.xml部署文件
services.xml
<?xml version="1.0" encoding="UTF-8"?>
//下面定义服务名
<service name="HelloWorld">
<description>
 This is a sample Web Service.
</description>
// ServiceClass指定 Java Class的位置,即实现服务的类。
<parameter name="ServiceClass" locked="false">sample.HelloWorld</parameter>
// operation 与 Java Class中方法名对应。
<operation name="sayHello">
// messageReceiver看下文注解。
 <messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
</operation>
</service>
 
注解: 消息交换模式。
目前 Axis2支持三种模式: In-Only、 Robust-In和 In-Out。 In-Only消息交换模式只有 SOAP请求,而不需要应答; Robust-In消息交换模式发送 SOAP请求,只有在出错的情况下才返回应答; In-Out消息交换模式总是存在 SOAP请求和应答。本例使用 In-Out模式。
 
4.客户端文件 TestClient.java
TestClient.java
package example.client;
 
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
 
public class TestClient {
    // targetEPR指定打包的 Service( .aar文件 ) 在容器中的物理位置。
       private static EndpointReference targetEPR=new EndpointReference
        ("http://localhost:8080/axis2/services/HelloWorld");
       public static OMElement getSayHelloOMElement(){
        //创建 request SOAP包。
              OMFactory fac=OMAbstractFactory.getOMFactory();
        // OMNamespace指定此 SOAP文档名称空间。
              OMNamespace omNs=fac.createOMNamespace("http://helloworld.com/","hw");
        //创建元素 sayHello,并指定其在 omNs指代的名称空间中。
              OMElement method=fac.createOMElement("sayHello",omNs);
        //指定元素的文本内容。
              method.setText("ZJ");
              return method;
       }
       public static void main(String[] args){
              try{
                     Options options=new Options();
                     options.setTo(targetEPR);
                     ServiceClient sender=new ServiceClient();
                     sender.setOptions(options);
                     OMElement sayHello=TestClient.getSayHelloOMElement();
            //发出 request SOAP,
//同时将得到的远端由 sayHello方法返回的信息保存到 result。
//通过 services.xml能准确找到 sayHello方法所在的文件。
                     OMElement result=sender.sendReceive(sayHello);
              }
              catch(Exception axisFault){
                     axisFault.printStackTrace();
              }
       }
}
 
5 .Axis2 简介
Apache Axis2 是 Axis的后续版本,是新一代的 SOAP引擎。 Axis2的主要特点有:
1)采用名为  AXIOM( AXIs Object Model)的新核心  XML 处理模型,利用新的 XML解析器提供的灵活性按需构造对象模型。
 
2)支持不同的消息交换模式。目前 Axis2支持三种模式: In-Only、 Robust-In和 In-Out。 In-Only消息交换模式只有 SOAP请求,而不需要应答; Robust-In消息交换模式发送 SOAP请求,只有在出错的情况下才返回应答; In-Out消息交换模式总是存在 SOAP请求和应答。
 
3)提供阻塞和非阻塞客户端  API。
 
4)支持内置的  Web服务寻址  (WS-Addressing) 。
 
5)灵活的数据绑定,可以选择直接使用  AXIOM,使用与原来的  Axis 相似的简单数据绑定方法,或使用  XMLBeans、 JiBX 或  JAXB 2.0 等专用数据绑定框架。
 
6)新的部署模型,支持热部署。
 
7)支持 HTTP, SMTP, JMS, TCP传输协议。
 
8)支持 REST (Representational State Transfer)。
 
6. Axis2 支持的规范包括:
-SOAP 1.1 and 1.2
-Message Transmission Optimization Mechanism (MTOM), XML Optimized Packaging (XOP) and SOAP with Attachments
-WSDL 1.1, including both SOAP and HTTP bindings
-WS-Addressing (submission and final)
-WS-Policy
-SAAJ 1.1
有关 Axis2更加详细的介绍,可以访问 Axis2网站 http://ws.apache.org/axis2。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值