杰瑞科技汇

Java Axis Webservice如何快速开发与调试?

Of course! Let's dive into creating and consuming a Java web service using the Apache Axis framework. Axis is a powerful and popular SOAP framework, and while it's considered somewhat legacy compared to modern REST frameworks, it's still used in many enterprise environments.

Java Axis Webservice如何快速开发与调试?-图1
(图片来源网络,侵删)

This guide will cover two main parts:

  1. Creating a Web Service (The Server): Building a Java application that exposes a method as a web service.
  2. Consuming a Web Service (The Client): Creating a Java application that calls the web service we just created.

What is Apache Axis?

Apache Axis is an open-source, XML-based Web Services engine. It allows you to:

  • Create web services from Java classes.
  • Deploy these services on a web server (like Tomcat).
  • Generate Java client stubs from a WSDL (Web Services Description Language) file to easily consume web services.

There are two main versions:

  • Axis 1: The original, stable, and widely used version. This is what we'll focus on.
  • Axis 2: A newer, more modular, and feature-rich rewrite. It has a different architecture and API.

Part 1: Creating a Web Service (The Server)

We will create a simple "Hello World" web service.

Java Axis Webservice如何快速开发与调试?-图2
(图片来源网络,侵删)

Step 1: Set Up Your Project

  1. Create a Java Project: In your favorite IDE (like Eclipse or IntelliJ), create a new Java project.
  2. Add Axis 1 Libraries: You need the Axis 1 JAR files in your project's classpath. You can download them from the Apache Axis 1 archive.
    • The essential JARs are in the lib directory of the Axis 1.4 distribution. Key ones include:
      • axis.jar
      • axis-ant.jar
      • commons-discovery.jar
      • commons-logging.jar
      • jaxrpc.jar
      • saaj.jar
      • wsdl4j.jar
      • log4j-1.2.xx.jar (for logging)

Step 2: Create the Service Endpoint Interface (SEI)

This is a simple Java interface with methods you want to expose. Annotate it with @WebService.

// src/com/example/HelloWorld.java
package com.example;
import javax.jws.WebService;
import javax.jws.WebMethod;
// @WebService marks this class as a Web Service
@WebService
public interface HelloWorld {
    // @WebMethod marks this method as a web service operation
    @WebMethod
    String sayHello(String name);
}

Step 3: Create the Service Implementation Class

This class implements the interface and provides the actual logic.

// src/com/example/HelloWorldImpl.java
package com.example;
public class HelloWorldImpl implements HelloWorld {
    @Override
    public String sayHello(String name) {
        if (name == null || name.trim().isEmpty()) {
            return "Hello, Stranger!";
        }
        return "Hello, " + name + "!";
    }
}

Step 4: Create the deploy.wsdd File

This is the Axis Deployment Descriptor. It's an XML file that tells Axis how to deploy your service. Place this file in the root of your project's source folder (src).

<!-- deploy.wsdd -->
<deployment xmlns="http://xml.apache.org/axis/wsdd/"
            xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
  <!-- This defines our service -->
  <service name="HelloWorld" provider="java:RPC">
    <!-- The fully qualified name of our implementation class -->
    <parameter name="className" value="com.example.HelloWorldImpl"/>
    <!-- The methods we want to expose -->
    <parameter name="allowedMethods" value="*"/>
    <!-- This is crucial for mapping the WSDL namespace -->
    <parameter name="typeMappingVersion" value="1.2"/>
    <!-- Specify the return types for the methods for better WSDL generation -->
    <beanMapping qname="myNS:HelloWorld" 
                 xmlns:myNS="urn:HelloWorld" 
                 languageSpecificType="java:com.example.HelloWorld"/>
  </service>
</deployment>

Step 5: Create the Server Application

This is a simple Java class with a main method to start the Axis server and deploy the service.

Java Axis Webservice如何快速开发与调试?-图3
(图片来源网络,侵删)
// src/com/example/Server.java
package com.example;
import org.apache.axis.server.AxisServer;
import org.apache.axis.configuration.FileProvider;
import java.io.File;
public class Server {
    public static void main(String[] args) throws Exception {
        // Path to the deploy.wsdd file
        String wsddPath = "deploy.wsdd";
        // Create a FileProvider to load the deployment configuration
        FileProvider provider = new FileProvider(new File(wsddPath));
        // Create a new AxisServer with our configuration
        AxisServer server = new AxisServer(provider);
        // Start the server
        server.start();
        System.out.println("Axis Server started and HelloWorld service deployed.");
        System.out.println("Press Enter to stop the server...");
        System.in.read(); // Wait for user input to keep the server running
        server.stop();
        System.out.println("Server stopped.");
    }
}

Step 6: Run the Server and Deploy the Service

  1. Make sure all the Axis JARs are in your classpath.
  2. Run the Server.java class.
  3. You should see the output: Axis Server started and HelloWorld service deployed.
  4. The service is now deployed and running! You can stop it by pressing Enter in the console.

Step 7: Test the Service

  1. Generate the WSDL: Open a web browser and navigate to: http://localhost:8080/axis/servlet/AxisServlet?wsdl (Note: Axis uses port 8080 by default. If you have another server running there, it might conflict.) You should see the XML content of the WSDL file. This describes your service.

  2. Test with a SOAP Client: You can use a tool like SoapUI or even curl to test the service endpoint. The endpoint URL is typically: http://localhost:8080/axis/services/HelloWorld

    Using curl:

    curl -X POST \
      http://localhost:8080/axis/services/HelloWorld \
      -H 'Content-Type: text/xml; charset=utf-8' \
      -H 'SOAPAction: ""' \
      -d '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:hel="urn:HelloWorld">
           <soapenv:Header/>
           <soapenv:Body>
              <hel:sayHello>
                 <name>World</name>
              </hel:sayHello>
           </soapenv:Body>
         </soapenv:Envelope>'

    You should get a SOAP response like this:

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
       <soapenv:Body>
          <ns1:sayHelloResponse xmlns:ns1="urn:HelloWorld">
             <ns1:sayHelloReturn>Hello, World!</ns1:sayHelloReturn>
          </ns1:sayHelloResponse>
       </soapenv:Body>
    </soapenv:Envelope>

Part 2: Consuming a Web Service (The Client)

Now, let's create a Java client that calls the HelloWorld service we just built.

Step 1: Generate the Client Stubs

Axis can automatically generate Java client code (stubs) from a WSDL file. This is the easiest way to consume a service.

  1. Get the WSDL URL: The URL to your service's WSDL is: http://localhost:8080/axis/services/HelloWorld?wsdl

  2. Use the WSDL2Java Tool: Axis provides a command-line tool for this. You need to run it from the command line.

    # Navigate to the directory containing axis.jar (e.g., the axis/lib folder)
    cd /path/to/axis-1.4/lib
    # Run the WSDL2Java command
    java -cp axis.jar:commons-logging.jar:wsdl4j.jar:commons-discovery.jar org.apache.axis.wsdl.WSDL2Java http://localhost:8080/axis/services/HelloWorld?wsdl
  3. Generated Files: This command will create a new directory structure, typically com/example, and generate several files inside it:

    • HelloWorld.java (The Service Interface)
    • HelloWorldService.java (The Locator class)
    • HelloWorldServiceLocator.java (The implementation of the locator)
    • HelloWorldSoapBindingStub.java (The client stub that handles the SOAP communication)

Step

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