杰瑞科技汇

Java webservice axis如何快速开发与部署?

Of course! Let's dive into creating a Java web service using Apache Axis. It's a classic technology that's great for understanding the fundamentals of SOAP web services, even though more modern frameworks like Spring Boot with JAX-RS are more common today.

Java webservice axis如何快速开发与部署?-图1
(图片来源网络,侵删)

This guide will cover:

  1. What is Apache Axis? (A brief overview)
  2. Step-by-Step Tutorial: Creating a simple "Hello World" web service.
  3. How to Deploy and Test the service.
  4. How to Create a Client to consume the service.
  5. Important Considerations: Pros, cons, and modern alternatives.

What is Apache Axis?

Apache Axis is an open-source, Java-based framework for creating and consuming SOAP (Simple Object Access Protocol) web services. It was one of the first widely adopted frameworks for this purpose and helped popularize the use of web services in the Java world.

There are two main versions:

  • Axis 1 (Legacy): The original version. It's relatively simple to use for basic services but has known limitations and is considered legacy.
  • Axis 2 (Current): A complete rewrite of Axis 1. It's more modular, performant, and standards-compliant (with JAX-WS), but has a steeper learning curve.

For this tutorial, we'll use Axis 1 because it's the most straightforward for a "getting started" example.

Java webservice axis如何快速开发与部署?-图2
(图片来源网络,侵删)

Step-by-Step Tutorial: Creating a "Hello World" Web Service with Axis 1

We will create a simple web service that takes a name as input and returns a greeting.

Prerequisites

  • Java Development Kit (JDK): Version 8 or higher.
  • Apache Ant: Axis 1 uses Ant for its build process. Make sure it's installed and in your PATH.
  • Apache Axis 1: Download the binary distribution from the Apache Axis 1 Archive. We'll use axis-bin-1_4.zip.

Step 1: Set Up the Project Structure

Create a new directory for your project, for example, AxisHelloWorld. Inside it, create the following structure:

AxisHelloWorld/
├── build.xml         (Ant build script - we will create this)
├── src/
│   └── com/
│       └── example/
│           └── webservice/
│               └── HelloWorld.java  (Our service implementation)
└── web/
    └── WEB-INF/
        └── web.xml                  (Deployment descriptor)

Step 2: Create the Service Implementation Class

This is the plain Java class that contains the business logic. Axis will use this to create the web service interface.

File: src/com/example/webservice/HelloWorld.java

Java webservice axis如何快速开发与部署?-图3
(图片来源网络,侵删)
package com.example.webservice;
// This is the implementation class for our web service.
public class HelloWorld {
    /**
     * A simple method that takes a name and returns a greeting.
     * @param name The name to greet.
     * @return A greeting string.
     */
    public String getGreeting(String name) {
        if (name == null || name.trim().isEmpty()) {
            return "Hello, Stranger!";
        }
        return "Hello, " + name + "!";
    }
}

Step 3: Create the Ant Build Script (build.xml)

This is the most crucial part. The build.xml file tells Ant how to compile your code, generate the web service artifacts (WSDL, server-side stubs), and package the WAR (Web Application Archive) file.

File: build.xml

<project name="AxisHelloWorld" default="deploy" basedir=".">
    <!-- Set up classpaths -->
    <path id="compile.classpath">
        <!-- The Axis 1 JARs -->
        <fileset dir="axis-1_4/lib" includes="**/*.jar"/>
        <!-- The Servlet API (usually provided by the web server) -->
        <fileset dir="axis-1_4/lib" includes="axis-ant.jar"/>
    </path>
    <!-- 1. COMPILE: Compile the source code -->
    <target name="compile" description="Compile the source files">
        <mkdir dir="build/classes"/>
        <javac srcdir="src" destdir="build/classes" includeantruntime="false">
            <classpath refid="compile.classpath"/>
        </javac>
    </target>
    <!-- 2. GENERATE: Generate WSDL and server-side skeletons -->
    <target name="generate" depends="compile" description="Generate WSDL and server-side skeletons">
        <!-- This task generates the WSDL and server-side Java files from the service class -->
        <axis-java2wsdl
            className="com.example.webservice.HelloWorld"
            location="http://localhost:8080/axis/services/HelloWorld"
            namespace="http://example.com/webservice"
            output="build/HelloWorld.wsdl"
            />
    </target>
    <!-- 3. PACKAGE: Create the WAR file -->
    <target name="package" depends="generate" description="Package the WAR file">
        <war destfile="dist/AxisHelloWorld.war" webxml="web/WEB-INF/web.xml">
            <!-- Add your compiled classes -->
            <lib dir="axis-1_4/lib">
                <include name="axis.jar"/>
                <include name="commons-discovery.jar"/>
                <include name="commons-logging.jar"/>
                <include name="jaxrpc.jar"/>
                <include name="saaj.jar"/>
                <include name="log4j-1.2.15.jar"/>
            </lib>
            <classes dir="build/classes"/>
            <!-- The generated WSDL file should be in the web services directory -->
            <fileset dir="build" includes="*.wsdl"/>
        </war>
    </target>
    <!-- 4. DEPLOY: Deploy the WAR file to a Tomcat server -->
    <target name="deploy" depends="package" description="Deploy the WAR to Tomcat">
        <!-- Assumes Tomcat is running on localhost:8080 and manager app is configured -->
        <deploy
            url="http://localhost:8080/manager/text"
            username="your_tomcat_user"
            password="your_tomcat_password"
            path="/AxisHelloWorld"
            warfile="file:${basedir}/dist/AxisHelloWorld.war"
        />
    </target>
</project>

Important: Before running this, you must:

  1. Extract axis-bin-1_4.zip into your project directory.
  2. Update the username and password in the deploy target to match your Tomcat manager credentials.

Step 4: Create the Deployment Descriptor (web.xml)

This file tells the servlet container (like Tomcat) how to handle requests for our web service.

File: web/WEB-INF/web.xml

<?xml version="1.1" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <!-- This is the standard Axis servlet -->
    <servlet>
        <servlet-name>AxisServlet</servlet-name>
        <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>
    </servlet>
    <!-- Map all requests to /services/* to the Axis servlet -->
    <servlet-mapping>
        <servlet-name>AxisServlet</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>
    <!-- Welcome file list -->
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>

How to Deploy and Test

  1. Start Tomcat: Make sure your Apache Tomcat server is running.

  2. Run Ant: Open a command prompt in your project's root directory (AxisHelloWorld) and run the Ant command:

    ant deploy

    This will execute the deploy target in build.xml, which in turn will compile, generate, package, and deploy your WAR file to Tomcat.

  3. Verify Deployment: Open your browser and go to the Axis service list page: http://localhost:8080/axis/ You should see AxisHelloWorld listed as a deployed service.

  4. Test the Service:

    • Click on the HelloWorld link in the service list.
    • This will take you to the service's testing page.
    • In the getGreeting operation's field, enter a name (e.g., "Alice").
    • Click the Invoke button.

    You should see an XML response like this:

    <ns1:getGreetingResponse xmlns:ns1="http://example.com/webservice">
      <ns1:getGreetingReturn>Hello, Alice!</ns1:getGreeting
分享:
扫描分享到社交APP
上一篇
下一篇