Apache Axis 1.4, while a bit older, remains a valuable tool for understanding the fundamentals of SOAP-based web services. In this post, we’ll walk through the process of creating a basic web service using Axis 1.4.
Prerequisites
- Java Development Kit (JDK): Make sure you have a JDK installed and configured.
- Apache Axis 1.4: Download the Axis 1.4 binary distribution from the Apache website.
- Apache Tomcat (or another servlet container): You’ll need a servlet container to deploy your web service.
1. Setting Up Axis 1.4
- Extract the Axis 1.4 distribution: Unzip the downloaded Axis 1.4 archive to a directory of your choice.
- Add Axis libraries to your classpath: You’ll need to add the JAR files in the
libdirectory of the Axis distribution to your project’s classpath. If you are using an IDE, you can add them through the project settings.
2. Creating the Web Service
Let’s create a simple Java class that will serve as our web service.
```java
// MyWebService.java
public class MyWebService {
public String sayHello(String name) {
return “Hello, ” + name + ”!”;
}
}
- Generating the WSDL
Axis 1.4 uses Java2WSDL to generate the Web Services Description Language (WSDL) file.
- Compile your Java class: Compile MyWebService.java to create MyWebService.class.
- Run Java2WSDL: Use the following command to generate the WSDL file:
java org.apache.axis.wsdl.Java2WSDL -o MyWebService.wsdl -l “http://localhost:8080/axis/services/MyWebService” -n “urn:MyNamespace” MyWebService - -o MyWebService.wsdl: Specifies the output file name.
- -l “http://localhost:8080/axis/services/MyWebService”: Sets the service endpoint URL.
- -n “urn:MyNamespace”: Defines the target namespace.
- MyWebService: The fully qualified name of your Java class.
- Creating the WSDD
The Web Service Deployment Descriptor (WSDD) file is used to configure the deployment of your web service.
http://xml.apache.org/axis/wsdd/” xmlns:java=” http://xml.apache.org/axis/wsdd/providers/java”> - Deploying the Web Service
- Copy the WSDD file: Place deploy.wsdd in the WEB-INF directory of your web application. If you don’t have a web application yet, create one.
- Create a WEB-INF/lib directory: Copy the Axis 1.4 JAR files from the Axis lib directory into the WEB-INF/lib directory of your web application.
- Deploy the web application: Deploy your web application to Tomcat or your chosen servlet container.
- Deploy the service using AdminClient: Use the Axis AdminClient to deploy the service:
java org.apache.axis.client.AdminClient deploy.wsdd Make sure you run this command from the WEB-INF directory of your web application, or specify the full path to deploy.wsdd.
If you are using a remote server, you may need to provide the URL to the axis admin service. For example:
java org.apache.axis.client.AdminClient http://localhost:8080/axis/services/AdminService deploy.wsdd
- Testing the Web Service
You can test your web service by creating a simple Java client or by using a tool like SoapUI.
Here’s a basic Java client example:
// MyWebServiceClient.java
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;
public class MyWebServiceClient {
public static void main(String[] args) throws Exception {
String endpoint = “http://localhost:8080/axis/services/MyWebService”;
Service service = new Service();
Call call = (Call) service.createCall(); call.setTargetEndpointAddress(new java.net.URL(endpoint)); call.setOperationName(new QName("urn:MyNamespace", "sayHello")); String result = (String) call.invoke(new Object[] { "World" }); System.out.println(result); }
}
Compile and run MyWebServiceClient.java to see the output.
Conclusion
This walkthrough provides a basic understanding of creating and deploying web services using Apache Axis 1.4. While newer technologies like JAX-WS and Spring Web Services offer more advanced features, Axis 1.4 remains a valuable tool for learning the fundamentals of SOAP-based web services.