`
tenn
  • 浏览: 565585 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
文章分类
社区版块
存档分类
最新评论

基于Tomcat5.0和Axis2开发Web Service代码详解

阅读更多
版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://zhangjunhd.51cto.com/113473/23692
本文将详细介绍HelloWorld中使用的serverclient端代码。阅读之前,你应该首先了解SOAP1.1协议。<o:p></o:p>
author: ZJ <st1:chsdate w:st="on" year="2007" month="3" day="13" islunardate="False" isrocdate="False">07-3-13</st1:chsdate>
<o:p> </o:p>
1HelloWorld做了些什么?<o:p></o:p>
HelloWorld功能非常简单,在客户端输入你的姓名,本例中为ZJ。参数传递到服务器端后,经过处理将返回name+"HelloWorld!",本例中为ZJ HelloWorld!
<o:p> </o:p>
2.服务器端文件HelloWorld.java<o:p></o:p>
HelloWorld.java<o:p></o:p>
package sample;
<o:p> </o:p>
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;<o:p></o:p>
<o:p> </o:p>
public class HelloWorld {<o:p></o:p>
    //读取clientgetSayHelloOMElement()方法传递的参数in<o:p></o:p>
       public OMElement sayHello(OMElement in){<o:p></o:p>
        //in转换为String<o:p></o:p>
              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;
       }
}
<o:p> </o:p>
3services.xml部署文件<o:p></o:p>
services.xml<o:p></o:p>
<?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>
<o:p> </o:p>
注解:消息交换模式。<o:p></o:p>
目前Axis2支持三种模式:In-OnlyRobust-InIn-OutIn-Only消息交换模式只有SOAP请求,而不需要应答;Robust-In消息交换模式发送SOAP请求,只有在出错的情况下才返回应答;In-Out消息交换模式总是存在SOAP请求和应答。本例使用In-Out模式。
<o:p> </o:p>
4.客户端文件TestClient.java<o:p></o:p>
TestClient.java<o:p></o:p>
package example.client;<o:p></o:p>
<o:p> </o:p>
import org.apache.axiom.om.OMAbstractFactory;<o:p></o:p>
import org.apache.axiom.om.OMElement;<o:p></o:p>
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;<o:p></o:p>
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;<o:p></o:p>
import org.apache.axis2.client.ServiceClient;<o:p></o:p>
<o:p> </o:p>
public class TestClient {<o:p></o:p>
    // targetEPR指定打包的Service.aar文件在容器中的物理位置。<o:p></o:p>
       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");<o:p></o:p>
        //创建元素sayHello,并指定其在omNs指代的名称空间中。<o:p></o:p>
              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();
              }
       }
}
<o:p> </o:p>
5Axis2简介<o:p></o:p>
Apache Axis2 Axis的后续版本,是新一代的SOAP引擎。Axis2的主要特点有:
1)采用名为 AXIOMAXIs Object Model)的新核心 XML 处理模型,利用新的XML解析器提供的灵活性按需构造对象模型。
<o:p> </o:p>
2)支持不同的消息交换模式。目前Axis2支持三种模式:In-OnlyRobust-InIn-OutIn-Only消息交换模式只有SOAP请求,而不需要应答;Robust-In消息交换模式发送SOAP请求,只有在出错的情况下才返回应答;In-Out消息交换模式总是存在SOAP请求和应答。
<o:p> </o:p>
3)提供阻塞和非阻塞客户端 API
<o:p> </o:p>
4)支持内置的 Web服务寻址 (WS-Addressing)
<o:p> </o:p>
5)灵活的数据绑定,可以选择直接使用 AXIOM,使用与原来的 Axis 相似的简单数据绑定方法,或使用 XMLBeansJiBX JAXB 2.0 等专用数据绑定框架。
<o:p> </o:p>
6)新的部署模型,支持热部署。
<o:p> </o:p>
7)支持HTTPSMTPJMSTCP传输协议。
<o:p> </o:p>
8)支持REST (Representational State Transfer)
<o:p> </o:p>
6Axis2 支持的规范包括:<o:p></o:p>
-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/
<o:p></o:p> 
<o:p>相关介绍 </o:p>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics