about 4 years ago

Apache Axis2

Apache Axis2 是一個 Web Service 的核心支援引擎. 能將撰寫好的服務放在 Apache Axis2 上運行,以提供客戶端呼叫使用!

0. 程式碼

1. 安裝

1.1. 下載 & 解壓縮

下載 Apache Axis2,最新版本的 WAR Distribution .ZIP 檔案

解壓縮 .zip 取得 axis2.war 接著步驟 1.2

1.2. 將 axis2.war 放置到 Tomcat/Webapp 目錄

1.3. 啟用 Tomcat

1.4. 檢視 Apache Axis2 是否啟動正常

http://127.0.0.1:8080/axis2/services/Weather?wsdl

2. 發佈服務

2.1. 建置服務專案

2.1.1. 開啟 Apache Maven 專案
2.1.2. 新增 src/main/resources 目錄
2.1.3. 新增 services.xml 至 src/main/resources 目錄
2.1.3.1. Code 示範
2.1.3.1.1. services.xml
services.xml
<service name="Weather" targetNamespace="http://www.w3.org/2005/08/addressing">
    <description>
        Weather
    </description>
    <parameter name="ServiceClass">
        edu.tcu.gaduo.Weather
    </parameter>
    <operation name="GetWeather">
        <actionMapping>http://www.webserviceX.NET/GetWeather</actionMapping>
        <messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver" />
    </operation>
</service>
2.1.3.1.2. Weather.java
Weather.java
package edu.tcu.gaduo;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.util.AXIOMUtil;

public class Weather {
    public OMElement GetWeather(OMElement request){
        String response = 
                    "<GetWeatherResult>" + 
                        "<CurrentWeather>" + 
                            "<Location>Sungshan / Taipei, Taiwan (RCSS) 25-04N 121-33E</Location>" + 
                            "<Time>Mar 25, 2014 - 12:00 PM EDT / 2014.03.25 1600 UTC</Time>" + 
                            "<Wind> from the E (100 degrees) at 5 MPH (4 KT):0</Wind>" + 
                            "<Visibility> 5 mile(s):0</Visibility>" + 
                            "<SkyConditions> mostly cloudy</SkyConditions>" + 
                            "<Temperature> 71 F (22 C)</Temperature>" + 
                            "<DewPoint> 62 F (17 C)</DewPoint>" +
                            "<RelativeHumidity> 73%</RelativeHumidity>" + 
                            "<Pressure> 29.94 in. Hg (1014 hPa)</Pressure>" + 
                            "<Status>Success</Status>" + 
                        "</CurrentWeather>" + 
                    "</GetWeatherResult>" ;
        
        OMElement element = null;
        try {
            element = AXIOMUtil.stringToOM(response);
        } catch (javax.xml.stream.XMLStreamException e) {
            e.printStackTrace();
        }
        return element;
    }
}
2.1.3.1.3. pom.xml
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>edu.tcu.gaduo</groupId>
    <artifactId>SampleProject</artifactId>
    <version>0.0.1</version>
    <packaging>jar</packaging>

    <name>SampleProject</name>
    <url>http://maven.apache.org</url>

    <properties>
        <axis2.version>1.6.2</axis2.version>
        <axiom.version>1.2.14</axiom.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.ws.commons.axiom</groupId>
            <artifactId>axiom-api</artifactId>
            <version>${axiom.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.ws.commons.axiom</groupId>
            <artifactId>axiom-dom</artifactId>
            <version>${axiom.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.ws.commons.axiom</groupId>
            <artifactId>axiom-impl</artifactId>
            <version>${axiom.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-adb</artifactId>
            <version>${axis2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-adb-codegen</artifactId>
            <version>${axis2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-ant-plugin</artifactId>
            <version>${axis2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-clustering</artifactId>
            <version>${axis2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-codegen</artifactId>
            <version>${axis2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-corba</artifactId>
            <version>${axis2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-fastinfoset</artifactId>
            <version>${axis2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-java2wsdl</artifactId>
            <version>${axis2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-jaxbri</artifactId>
            <version>${axis2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-jaxws</artifactId>
            <version>${axis2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-jibx</artifactId>
            <version>${axis2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-json</artifactId>
            <version>${axis2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-kernel</artifactId>
            <version>${axis2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-metadata</artifactId>
            <version>${axis2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-mtompolicy</artifactId>
            <version>${axis2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-saaj</artifactId>
            <version>${axis2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-soapmonitor-servlet</artifactId>
            <version>${axis2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-spring</artifactId>
            <version>${axis2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-transport-http</artifactId>
            <version>${axis2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-transport-local</artifactId>
            <version>${axis2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-xmlbeans</artifactId>
            <version>${axis2.version}</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.axis2</groupId>
                <artifactId>axis2-java2wsdl-maven-plugin</artifactId>
                <configuration>
                    <className>edu.tcu.gaduo.Weather</className>
                    <outputFileName>${project.build.directory}/Weather.wsdl</outputFileName>
                </configuration>
                <executions>
                    <execution>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>java2wsdl</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.axis2</groupId>
                <artifactId>axis2-aar-maven-plugin</artifactId>
                <configuration>
                    <servicesXmlFile>${basedir}/src/main/resources/services.xml</servicesXmlFile>
                    <wsdlFile>${project.build.directory}/Weather.wsdl</wsdlFile>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>aar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

2.1.4. 將服務發佈至 axis2 上運行

2.1.4.1. 輸入指令建置

開啟 Command Line
cd 至專案根目錄輸入以下指令

mvn
mvn clean install -DskipTests
2.1.4.2. 發佈服務

至專案下 target 目錄,複製 .aar 檔至 $TOMCAT_HOME\webapps\axis2\WEB-INF\services 目錄下

2.1.4.3. 檢視服務是否發佈正常

服務清單 http://127.0.0.1:8080/axis2/services/listServices
Weather 服務 http://127.0.0.1:8080/axis2/services/Weather?wsdl

3. 測試伺服器端

3.1. 請實作先前文章

3.1.1. Java 網路程式 Web Service SOAP 伺服器端- HTTP Post 版
3.1.2. C# 網路程式 Web Service SOAP 客戶端 - HTTP Post 版
← Apache Maven - 如何建置 Apache Maven 專案 Android - Play 上架說明 →
 
comments powered by Disqus