首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Tomcat 5.0 與 Axis2 實戰部署:從環境建置到 AAR 封裝與驗證

     編輯:Java 相關知識

Axis2 Web Service 完整部署實戰教學

本文記錄在 Eclipse + Tomcat 5.0 環境下,使用 Axis2 1.0 版本建立、封裝、部署與測試 Web Service 的完整流程,適合初學者快速上手。

開發環境準備

  • Eclipse 3.1.2 + Lomboz
  • JDK 1.5
  • Apache Tomcat 5.0.18
  • Axis2 1.0 (Binary Distribution + WAR Distribution)

環境設定步驟

  1. 下載並解壓 Axis2 Binary 版本至 C:\axis2-std-1.0-bin
  2. 在 Eclipse 中建立 User Library「axis2」,包含 C:\axis2-std-1.0-bin\lib 所有 JAR
  3. 將 axis2.war 複製至 %TOMCAT_HOME%/webapps 目錄

安裝驗證

  1. 啟動 Tomcat,訪問 https://localhost:8080/axis2/
  2. 點擊「Validate」連結進入 Axis2 Happiness Page
  3. Axis2 首頁
  4. Axis2 Happiness Page

HelloWorld 服務開發流程

1. 建立動態 Web 專案

專案名:ZZaxis,加入 axis2 User Library

2. 實作服務類 (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("https://helloworld.com/", "hw");
        OMElement resp = fac.createOMElement("sayHelloResponse", omNs);
        resp.setText(info);
        return resp;
    }
}

3. 建立部署描述檔 (WebContent/META-INF/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. 目錄結構與 AAR 封裝

+-example
|-------- +-sample
|------- HelloWorld.class
|---------+-META-INF
|------- services.xml

命令列執行封裝:

cd example
jar cvf HelloWorld.aar .

AAR 封裝執行畫面

5. 透過管理介面部署

  1. 訪問 https://localhost:8080/axis2/ 進入 Administration
  2. 登入 (admin/axis2)
  3. Tools → Upload Service 上傳 HelloWorld.aar
  4. 檔案將部署至 <CATALINA_HOME>/webapps/axis2/WEB-INF/services

客戶端測試專案

建立 Java 專案 ZZaxisClient

同樣加入 axis2 User Library

測試代碼 (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(
        "https://localhost:8080/axis2/services/HelloWorld");
    
    public static OMElement getSayHelloOMElement() {
        OMFactory fac = OMAbstractFactory.getOMFactory();
        OMNamespace omNs = fac.createOMNamespace("https://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 = getSayHelloOMElement();
            OMElement result = sender.sendReceive(sayHello);
            System.out.println(result);
        } catch (Exception axisFault) {
            axisFault.printStackTrace();
        }
    }
}

執行結果驗證

<hw:sayHelloResponse xmlns:hw="https://helloworld.com/" xmlns:tns="https://ws.apache.org/axis2">
ZJHelloWorld!
</hw:sayHelloResponse>

本文參考自「子孑」博客:原文連結

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved