1. 程式人生 > >基於Axis2開發WebService的HelloWorld(一)

基於Axis2開發WebService的HelloWorld(一)

1.HelloWorld做了些什麼?
HelloWorld功能非常簡單,在客戶端輸入你的姓名,本例中為Leo。引數傳遞到伺服器端後,經過處理將返回name+"HelloWorld!",本例中為Leo HelloWorld!

2.伺服器端檔案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("Leo");
              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/。