<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-72  評(píng)論-20  文章-0  trackbacks-1
    本文將介紹如何使用Tomcat5.0和Apache Axis2開發(fā)、部署及測(cè)試一個(gè)簡(jiǎn)單的Web Service應(yīng)用。
    author: ZJ
     
    1工作環(huán)境
    Eclipse
    http://ws.apache.org/axis2/download/1_0/download.cgi頁面下,下載AXIS2的Binary Distribution url: http://apache.justdn.org/ws/axis2/1_0/axis2-std-1.0-bin.zip和war Distribution url: http://apache.justdn.org/ws/axis2/1_0/axis2-1.0-docs.zip。把這兩個(gè)文件解壓,比如解壓縮的后得目錄為C:\axis2-std-1.0-bin和C:\axis2.war。
    在Eclipse下通過菜單window—preferences…--Java—Build Path—User Libraries 新建一個(gè)user library,比如名字就叫axis2把C:\axis2-std-1.0-bin\lib下的所有jar文件包含進(jìn)來。把a(bǔ)xis2.war拷貝到%TOMCAT-HOME%/webapps下面。
     
    2.檢驗(yàn)安裝
    在Eclipse下啟動(dòng)Tomcat,在地址欄內(nèi)輸入http://localhost:8080/axis2/。
    點(diǎn)擊Validate,將到達(dá) Axis2 Happiness Page。
    3.WebService中的HelloWorld
    1)新建一個(gè)動(dòng)態(tài)web工程,取名ZZaxis,右鍵點(diǎn)擊項(xiàng)目名,選擇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文件。
    在命令符環(huán)境下,將當(dāng)前目錄轉(zhuǎn)到example。
    jar cvf HelloWorld.aar . //注意最后一個(gè)點(diǎn),在當(dāng)前目錄下生成HelloWorld.aar。
     
    6)在Eclipse中啟動(dòng)Tomcat,在地址欄下鍵入http://localhost:8080/axis2/。選擇Administration,輸入用戶名admin,密碼axis2。選擇左側(cè)工具欄Tools- Upload Service,上傳之前打包的HelloWorld.aar。該文件將在<CATALINA_HOME>/webapps/axis2\WEB-INF\services目錄下。
     
    7)編寫客戶端檢驗(yàn)代碼。新建Java Project,取名為ZZaxisClient。右鍵點(diǎn)擊項(xiàng)目名,選擇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)測(cè)試,run TestClient.java as Java Application。結(jié)果:
    <hw:sayHelloResponse xmlns:hw="http://helloworld.com/"
    xmlns:tns="http://ws.apache.org/axis2">
    ZJHelloWorld!
    </hw:sayHelloResponse>
     
    4.后續(xù)
    詳細(xì)介紹client和server端代碼。
    代碼實(shí)例:

    1.HelloWorld做了些什么?
    HelloWorld功能非常簡(jiǎn)單,在客戶端輸入你的姓名,本例中為ZJ。參數(shù)傳遞到服務(wù)器端后,經(jīng)過處理將返回name+"HelloWorld!",本例中為ZJ HelloWorld!
     
    2.服務(wù)器端文件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()方法傳遞的參數(shù)in。
           public OMElement sayHello(OMElement in){
            //將in轉(zhuǎn)換為String。
                  String name=in.getText();
                  String info=name+"HelloWorld!";
            //創(chuàng)建response SOAP包。
                  OMFactory fac=OMAbstractFactory.getOMFactory();
            // OMNamespace指定此SOAP文檔名稱空間。
                  OMNamespace omNs=fac.createOMNamespace("http://helloworld.com/","hw");
            //創(chuàng)建元素sayHello,并指定其在omNs指代的名稱空間中。
                  OMElement resp=fac.createOMElement("sayHelloResponse",omNs);
            //指定元素的文本內(nèi)容。
                  resp.setText(info);
                  return resp;
           }
    }
     
    3.services.xml部署文件
    services.xml
    <?xml version="1.0" encoding="UTF-8"?>
    //下面定義服務(wù)名
    <service name="HelloWorld">
    <description>
      This is a sample Web Service.
    </description>
    // ServiceClass指定Java Class的位置,即實(shí)現(xiàn)服務(wù)的類。
    <parameter name="ServiceClass" locked="false">sample.HelloWorld</parameter>
    // operation 與Java Class中方法名對(duì)應(yīng)。
    <operation name="sayHello">
    // messageReceiver看下文注解。
      <messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
    </operation>
    </service>
     
    注解:消息交換模式。
    目前Axis2支持三種模式:In-Only、Robust-In和In-Out。In-Only消息交換模式只有SOAP請(qǐng)求,而不需要應(yīng)答;Robust-In消息交換模式發(fā)送SOAP請(qǐng)求,只有在出錯(cuò)的情況下才返回應(yīng)答;In-Out消息交換模式總是存在SOAP請(qǐng)求和應(yīng)答。本例使用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(){
            //創(chuàng)建request SOAP包。
                  OMFactory fac=OMAbstractFactory.getOMFactory();
            // OMNamespace指定此SOAP文檔名稱空間。
                  OMNamespace omNs=fac.createOMNamespace("http://helloworld.com/","hw");
            //創(chuàng)建元素sayHello,并指定其在omNs指代的名稱空間中。
                  OMElement method=fac.createOMElement("sayHello",omNs);
            //指定元素的文本內(nèi)容。
                  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();
                //發(fā)出request SOAP,
    //同時(shí)將得到的遠(yuǎn)端由sayHello方法返回的信息保存到result。
    //通過services.xml能準(zhǔn)確找到sayHello方法所在的文件。
                         OMElement result=sender.sendReceive(sayHello);
                  }
                  catch(Exception axisFault){
                         axisFault.printStackTrace();
                  }
           }
    }
     
    5.Axis2簡(jiǎn)介
    Apache Axis2 是Axis的后續(xù)版本,是新一代的SOAP引擎。Axis2的主要特點(diǎn)有:
    1)采用名為 AXIOM(AXIs Object Model)的新核心 XML 處理模型,利用新的XML解析器提供的靈活性按需構(gòu)造對(duì)象模型。
     
    2)支持不同的消息交換模式。目前Axis2支持三種模式:In-Only、Robust-In和In-Out。In-Only消息交換模式只有SOAP請(qǐng)求,而不需要應(yīng)答;Robust-In消息交換模式發(fā)送SOAP請(qǐng)求,只有在出錯(cuò)的情況下才返回應(yīng)答;In-Out消息交換模式總是存在SOAP請(qǐng)求和應(yīng)答。
     
    3)提供阻塞和非阻塞客戶端 API。
     
    4)支持內(nèi)置的 Web服務(wù)尋址 (WS-Addressing) 。
     
    5)靈活的數(shù)據(jù)綁定,可以選擇直接使用 AXIOM,使用與原來的 Axis 相似的簡(jiǎn)單數(shù)據(jù)綁定方法,或使用 XMLBeans、JiBX 或 JAXB 2.0 等專用數(shù)據(jù)綁定框架。
     
    6)新的部署模型,支持熱部署。
     
    7)支持HTTP,SMTP,JMS,TCP傳輸協(xié)議。
     
    8)支持REST (Representational State Transfer)。
     
    6.Axis2 支持的規(guī)范包括:
    -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
    有關(guān)Axis2更加詳細(xì)的介紹,可以訪問Axis2網(wǎng)站http://ws.apache.org/axis2/。


    posted on 2007-08-19 04:52 前方的路 閱讀(2493) 評(píng)論(0)  編輯  收藏 所屬分類: Java技術(shù)
    主站蜘蛛池模板: 免费观看亚洲人成网站| 一级毛片试看60分钟免费播放| 免费国产在线精品一区| 中文字幕亚洲免费无线观看日本| 成人免费视频一区| 国产精品亚洲аv无码播放| 亚洲深深色噜噜狠狠网站| 丝瓜app免费下载网址进入ios| 丁香花免费高清视频完整版| 亚洲伊人久久综合中文成人网| 亚洲成人午夜电影| 国产高潮流白浆喷水免费A片 | 日韩在线免费视频| 亚洲色精品aⅴ一区区三区| 亚洲偷自拍另类图片二区| 青柠影视在线观看免费| 免费看AV毛片一区二区三区| 亚洲AV美女一区二区三区| 在线观看亚洲精品专区| 国产在线观看麻豆91精品免费 | 大学生美女毛片免费视频| 亚洲精品国产品国语在线| 无码一区二区三区亚洲人妻| 美丽姑娘免费观看在线观看中文版| 国产乱子影视频上线免费观看| 亚洲精品美女在线观看| XXX2高清在线观看免费视频| 午夜视频在线在免费| 91天堂素人精品系列全集亚洲| 一级人做人a爰免费视频 | 最近最新MV在线观看免费高清| 国产亚洲精AA在线观看SEE| 美女被暴羞羞免费视频| 成人免费看黄20分钟| 亚洲网红精品大秀在线观看| 国产免费区在线观看十分钟| 好爽好紧好大的免费视频国产| 亚洲中文字幕人成乱码| 久久国产乱子伦精品免费不卡| 亚洲伊人久久成综合人影院| 国产亚洲漂亮白嫩美女在线|