杰瑞科技汇

Axis2教程,如何快速入门与实战?

Axis2 WebService 完整教程

目录

  1. 第一部分:准备工作与环境搭建

    Axis2教程,如何快速入门与实战?-图1
    (图片来源网络,侵删)
    • 1 什么是 Axis2?
    • 2 为什么选择 Axis2?
    • 3 环境要求
    • 4 创建 Maven Web 项目
  2. 第二部分:发布第一个简单的 WebService

    • 1 创建服务端接口和实现类
    • 2 配置 services.xml
    • 3 打包成 AAR 文件
    • 4 部署到 Axis2 服务器
    • 5 测试 WebService
  3. 第三部分:客户端调用 WebService

    • 1 使用 Axis2 客户端调用
    • 2 使用 wsdl4j 动态调用
  4. 第四部分:进阶主题

    • 1 传递复杂数据(POJO)
    • 2 传递和接收 List 集合
    • 3 处理 XML 文件作为参数
  5. 第五部分:常见问题与总结

    Axis2教程,如何快速入门与实战?-图2
    (图片来源网络,侵删)

第一部分:准备工作与环境搭建

1 什么是 Axis2?

Apache Axis2 是一个开源的、高性能的、功能强大的 WebService 引擎和 SOAP 框架,它是由 Apache 软件基金会开发的,是 Axis1.x 的下一代产品,Axis2 支持最新的 WebService 标准,如 SOAP 1.2、WSDL 2.0 和 WS-* 规范。

2 为什么选择 Axis2?

  • 高性能:采用 AXIOM (AXIs Object Model) 处理 SOAP 消息,性能优于 DOM。
  • 模块化架构:核心功能与扩展功能分离,易于扩展和维护。
  • 数据绑定灵活:支持 ADB (Axis Data Binding)、XMLBeans 和 JiBX 等多种数据绑定方式。
  • *全面的 WS- 支持**:支持 WS-Security、WS-Addressing 等企业级特性。
  • 优秀的工具支持:提供了 WSDL2JavaJava2WSDL 等命令行工具,极大简化了开发。

3 环境要求

  • JDK 8 或更高版本
  • Apache Maven 3.6+
  • IDE (推荐 IntelliJ IDEA 或 Eclipse)

4 创建 Maven Web 项目

  1. 打开你的 IDE,创建一个新的 Maven 项目。

  2. 选择 maven-archetype-webapp 模板。

  3. 填写 GroupId ( com.example) 和 ArtifactId ( axis2-demo)。

    Axis2教程,如何快速入门与实战?-图3
    (图片来源网络,侵删)
  4. 创建成功后,项目结构如下:

    axis2-demo
    ├── src
    │   └── main
    │       ├── java
    │       │   └── com
    │       │       └── example
    │       ├── resources
    │       └── webapp
    │           ├── WEB-INF
    │           │   └── web.xml
    │           └── index.jsp
    └── pom.xml
  5. 配置 pom.xml:我们需要添加 Axis2 的核心依赖。

    <dependencies>
        <!-- Axis2 核心 -->
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-kernel</artifactId>
            <version>1.7.9</version> <!-- 使用较新的稳定版本 -->
        </dependency>
        <!-- Axis2 的 WAR 模块,用于在 Tomcat 等容器中部署 -->
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-webapp</artifactId>
            <version>1.7.9</version>
            <type>pom</type>
        </dependency>
        <!-- 用于将 Java 对象序列化为 XML,需要用到 -->
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-adb</artifactId>
            <version>1.7.9</version>
        </dependency>
        <!-- JUnit 测试 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

第二部分:发布第一个简单的 WebService

我们将创建一个简单的加法计算器服务。

1 创建服务端接口和实现类

src/main/java/com/example 目录下创建服务接口和实现类。

Calculator.java (接口)

package com.example;
import javax.jws.WebService;
import javax.jws.WebMethod;
// @WebService 注解表明这是一个 WebService 接口
@WebService
public interface Calculator {
    // @WebMethod 注解表明这是一个可以被远程调用的方法
    @WebMethod
    public int add(int a, int b);
}

CalculatorImpl.java (实现类)

package com.example;
import javax.jws.WebService;
// @WebService 注解的 endpointInterface 属性指向接口
@WebService(endpointInterface = "com.example.Calculator")
public class CalculatorImpl implements Calculator {
    @Override
    public int add(int a, int b) {
        System.out.println("WebService is called with a=" + a + ", b=" + b);
        return a + b;
    }
}

2 配置 services.xml

services.xml 是 Axis2 服务的部署描述符,它告诉 Axis2 如何处理我们的服务。

  1. src/main/webapp/WEB-INF 目录下创建一个名为 services 的文件夹。
  2. services 文件夹内创建 services.xml 文件。

src/main/webapp/WEB-INF/services/services.xml

<?xml version="1.0" encoding="UTF-8"?>
<service name="CalculatorService">
    <!-- description 是服务的描述 -->
    <description>
        A simple Calculator WebService
    </description>
    <!-- serviceClass 指向服务的实现类 -->
    <serviceClass>com.example.CalculatorImpl</serviceClass>
    <!-- messageReceivers 指定消息处理器,我们使用 ADB 方式 -->
    <messageReceivers>
        <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageInOutReceiver"/>
    </messageReceivers>
</service>

3 打包成 AAR 文件

Axis2 服务被打包成 .aar (Axis2 Archive) 文件进行部署。

  1. pom.xml 中添加 maven-assembly-plugin 用于生成 AAR 文件。

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.3.0</version>
                <configuration>
                    <descriptors>
                        <descriptor>src/main/assembly/aar.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-aar</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
  2. src/main 目录下创建 assembly 文件夹,并在其中创建 aar.xml 文件。

    src/main/assembly/aar.xml

    <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 http://maven.apache.org/xsd/assembly-2.1.0.xsd">
        <id>aar</id>
        <formats>
            <format>aar</format>
        </formats>
        <includeBaseDirectory>false</includeBaseDirectory>
        <fileSets>
            <fileSet>
                <directory>${project.build.directory}/classes</directory>
                <outputDirectory>/</outputDirectory>
            </fileSet>
            <fileSet>
                <directory>${basedir}/src/main/webapp/WEB-INF/services</directory>
                <outputDirectory>/META-INF</outputDirectory>
            </fileSet>
        </fileSets>
    </assembly>
  3. 执行 Maven 命令:mvn clean package。 4

分享:
扫描分享到社交APP
上一篇
下一篇