Share
Explore

Lab Exercise: Using wsdl2java to Build and Test a Simple SOAP Client


In this lab, you will use the Apache CXF tool wsdl2java to generate a SOAP client from a WSDL file and call a SOAP web service.
This step-by-step exercise will guide you through creating a simple SOAP server, generating the client code, and testing the result.
The workflow is designed to ensure that each step is clear, with an obvious result at the end.

Learning Objectives

Understand how to use the wsdl2java tool from Apache CXF.
Learn to consume a SOAP web service using the client code generated by wsdl2java.
Observe the interaction between a SOAP client and a SOAP service.

Prerequisites

Apache CXF is installed and configured (wsdl2java is accessible from the command line).
Java 17 is installed and configured (java and javac commands work).
A working directory is set up for the project (e.g., C:\SOAPLab).

Step-by-Step Instructions

Step 1: Set Up Your Project Structure

Open a terminal or command prompt and navigate to your working directory:
cd C:\SOAPLab

Create a project structure:
mkdir src
mkdir output

Step 2: Write a Simple SOAP Web Service

Open your code editor (e.g., Sublime Text) and create a new file HelloWorldService.java in the src folder:
package lab.soap;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;

@WebService
public class HelloWorldService {
@WebMethod
public String sayHello(String name) {
return "Hello, " + name + "! Welcome to SOAP services.";
}

public static void main(String[] args) {
Endpoint.publish("http://localhost:8080/soap/hello", new HelloWorldService());
System.out.println("Service running at http://localhost:8080/soap/hello?wsdl");
}
}

Compile and run the service:
Compile:
bash
Copy code
javac -d . src/lab/soap/HelloWorldService.java

Run the service:
bash
Copy code
java lab.soap.HelloWorldService

Open a browser and navigate to http://localhost:8080/soap/hello?wsdl. You should see the WSDL file.

Step 3: Use wsdl2java to Generate Client Code

Open a new terminal or command prompt (keep the SOAP service running).
Run the wsdl2java tool:
bash
Copy code
wsdl2java -d output http://localhost:8080/soap/hello?wsdl

Examine the output directory. You should see:
A package structure mirroring the service's namespace.
Java files for the service interface and implementation stubs.

Step 4: Write the SOAP Client

Create a new file SoapClient.java in the src folder:
java
Copy code
package lab.soap.client;

import lab.soap.HelloWorldService;
import lab.soap.HelloWorld;

public class SoapClient {
public static void main(String[] args) {
HelloWorldService service = new HelloWorldService();
HelloWorld hello = service.getHelloWorldPort();

String response = hello.sayHello("Peter");
System.out.println(response);
}
}

Compile the client:
bash
Copy code
javac -d . -cp output src/lab/soap/client/SoapClient.java

Step 5: Run the Client

Run the client program:
bash
Copy code
java -cp .;output lab.soap.client.SoapClient

Observe the output in the terminal:
text
Copy code
Hello, Peter! Welcome to SOAP services.

Results

You have created a SOAP service and tested it by calling it from a SOAP client.
The wsdl2java tool automatically generated the client stubs for you, saving significant development time.

Lab Deliverables

Submit the following:
All .java files (both the server and client).
Screenshots of:
The WSDL displayed in the browser.
The client terminal output.
Document any errors you encountered and how you resolved them.

Reflection

Using wsdl2java, you automated the process of generating SOAP client code. This tool demonstrates the power of WSDL in enabling seamless communication between systems, a cornerstone of Service-Oriented Architecture (SOA).
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.