Of course! This is a classic and important topic in Java development. I'll guide you through the concept of web services using XML, from the basic principles to practical code examples using the standard Java API (JAX-WS).

What is a Web Service?
A web service is a method of communication between two electronic devices over a network. It's a software system designed to support interoperable machine-to-machine interaction over a network. In the context of XML, this means the messages exchanged between the client and the server are formatted in XML.
Key Characteristics:
- Interoperability: Can be used by any client, regardless of the technology (Java, .NET, Python, etc.).
- XML-Based: Messages are structured in XML, making them human-readable and platform-independent.
- Standardized Protocols: Typically uses protocols like SOAP (Simple Object Access Protocol) over HTTP.
The Main Approach: SOAP with JAX-WS
The most common and standardized way to create XML-based web services in Java is using SOAP (Simple Object Access Protocol) and the JAX-WS (Java API for XML Web Services) specification.
- SOAP: A protocol for exchanging structured information in web services. A SOAP message is an XML document.
- JAX-WS: The Java standard API for creating SOAP web services and clients. It's built into the Java Development Kit (JDK), so you don't need any external libraries for the basic functionality.
How it works:

- You write a standard Java POJO (Plain Old Java Object) with your business logic.
- You use annotations from the
javax.jwspackage to mark this class as a web service. - A JAX-WS implementation (like the one built into the JDK) reads these annotations and automatically generates the necessary WSDL file and handles the conversion between Java method calls and SOAP XML messages.
Step-by-Step Example: Creating a SOAP Web Service
Let's create a simple "Hello World" web service.
Step 1: Create the Java Service Endpoint Interface (SEI)
This is a Java interface that defines the methods your web service will expose. It's not strictly required (you can annotate the implementation class directly), but it's a best practice.
src/com/example/HelloWorld.java
package com.example;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.WebParam;
// @WebService marks this class as a web service interface.
@WebService
public interface HelloWorld {
// @WebMethod marks a method as a web service operation.
// @WebParam specifies the name of the parameter in the SOAP message.
@WebMethod
String sayHello(@WebParam(name = "name") String name);
}
Step 2: Create the Service Implementation
This class implements the interface and contains the actual business logic.

src/com/example/HelloWorldImpl.java
package com.example;
import javax.jws.WebService;
// @WebService with endpointInterface links this implementation to the SEI.
@WebService(endpointInterface = "com.example.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
@Override
public String sayHello(String name) {
// Simple business logic
return "Hello, " + name + "!";
}
}
Step 3: Create the Publisher (to deploy the service)
This class uses the JAX-WS API to publish our service implementation on a specific URL.
src/com/example/HelloWorldPublisher.java
package com.example;
import javax.xml.ws.Endpoint;
// This class publishes our web service.
public class HelloWorldPublisher {
public static void main(String[] args) {
// The URL where the service will be available.
String url = "http://localhost:9999/ws/hello";
// Publish the service implementation at the given URL.
Endpoint.publish(url, new HelloWorldImpl());
System.out.println("Service is published at: " + url);
}
}
Step 4: Compile and Run the Publisher
- Make sure you have a JDK installed.
- Compile the Java files:
javac -d . src/com/example/*.java
- Run the publisher:
java com.example.HelloWorldPublisher
You should see the output:
Service is published at: http://localhost:9999/ws/hello
Your web service is now running!
Testing the Web Service
A. Check the WSDL (Web Services Description Language)
The WSDL is an XML file that describes your web service—its location, the operations it supports, and the format of the messages. JAX-WS generates this automatically.
Open your web browser and navigate to:
http://localhost:9999/ws/hello?wsdl
You will see a large XML document. This is the contract that a client will use to interact with your service.
B. Create a Java Client
Now, let's create a Java client to consume this service. JAX-WS makes this incredibly easy.
src/com/example/HelloWorldClient.java
package com.example;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import java.net.URL;
public class HelloWorldClient {
public static void main(String[] args) throws Exception {
// The URL of the WSDL file.
URL wsdlUrl = new URL("http://localhost:9999/ws/hello?wsdl");
// The qualified name of the service.
// The first part is the namespace URI from the WSDL.
// The second part is the service name from the WSDL.
QName qname = new QName("http://example.com/", "HelloWorldImplService");
// Create a service instance.
Service service = Service.create(wsdlUrl, qname);
// Get the port (the interface to the service).
HelloWorld helloWorld = service.getPort(HelloWorld.class);
// Now, you can call the web service methods as if it were a local object.
String response = helloWorld.sayHello("XML User");
System.out.println(response); // Output: Hello, XML User!
}
}
Compile and run the client:
javac -d . src/com/example/*.java java com.example.HelloWorldClient
You will see the output: Hello, XML User!
Understanding the XML (SOAP) Traffic
So, where is the XML? The JAX-WS API hides it from you, but it's happening under the hood. You can see it by using a tool like SoapUI or by enabling logging.
When the client calls helloWorld.sayHello("XML User"), it sends a SOAP request to the server. The request looks something like this:
SOAP Request (XML):
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:sayHello xmlns:ns2="http://example.com/">
<name>XML User</name>
</ns2:sayHello>
</soap:Body>
</soap:Envelope>
The server processes this, calls your Java method, and sends back a SOAP response:
SOAP Response (XML):
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:sayHelloResponse xmlns:ns2="http://example.com/">
<return>Hello, XML User!</return>
</ns2:sayHelloResponse>
</soap:Body>
</soap:Envelope>
Modern Alternatives
While SOAP/JAX-WS is robust and standardized, it can be complex and verbose. For many modern applications, especially public APIs, simpler alternatives are preferred.
- RESTful Web Services with JSON: This is the dominant paradigm today.
- Protocol: Uses standard HTTP methods (GET, POST, PUT, DELETE).
- Data Format: Uses JSON (JavaScript Object Notation) instead of XML. JSON is lighter and easier for machines to parse.
- Java APIs: JAX-RS (Java API for RESTful Web Services). The most common implementation is Jersey (from Oracle) or RESTEasy (from JBoss).
When to use what:
| Feature | SOAP / JAX-WS | REST / JAX-RS |
|---|---|---|
| Protocol | Custom SOAP over HTTP | Standard HTTP (GET, POST, etc.) |
| Data Format | XML (Strictly defined by WSDL) | JSON, XML, Text, etc. (Usually JSON) |
| Standard | Highly standardized (WS-*) | Less standardized, more flexible |
| Security | Built-in, standardized (WS-Security) | Relies on standard HTTP/HTTPS security |
| Complexity | More complex, verbose | Simpler, easier to understand |
| Best For | Enterprise systems, financial transactions, AS/2 integration, where strict contracts and security are paramount. | Public APIs, Mobile apps, Modern web applications, Microservices. |
Summary
- Web Service: A way for applications to communicate over a network.
