SOAP – Structure of a SOAP Message (JAX-WS)
SOAP (originally Simple Object Access Protocol) sets rules for messages sent between programs. A message uses XML (Extensible Markup Language), a text format with tags. Its outer container is an Envelope. Its Body holds the request or reply.
JAX-WS (Java API for XML Web Services) helps Java programs create and use SOAP services. It can turn Java values into XML messages and turn replies back into Java values.
- Envelope = the parcel box. Everything goes inside it.
- Header = the label on top: "Fragile", tracking number, who is allowed to open it. Optional.
- Body = the actual item inside – the thing you really wanted to send.
- Fault = a "return to sender" note explaining what went wrong.
SOAP is a standard set of rules for exchanging XML messages between programs over a network. The programs can use different languages and operating systems.
Features of SOAP
- XML-based – people can read the messages, and they can be checked against a schema (a set of rules).
- Transport independent – usually sent with HTTP (Hypertext Transfer Protocol) POST, but can also travel by email (SMTP), JMS queues or plain TCP (Transmission Control Protocol).
- Extensible – extra features are added through the Header: security (WS-Security), transactions (WS-AtomicTransaction), addressing (WS-Addressing) and reliable delivery.
- Built-in error handling – a standard
<Fault>element. - Contract-first – described by WSDL (Web Services Description Language), so client code can be generated automatically.
Structure of a SOAP message
flowchart TB
subgraph HTTP[HTTP POST request]
direction TB
H1["Headers: Content-Type: text/xml; SOAPAction: ..."]
subgraph E[soap:Envelope – mandatory root]
direction TB
HD[soap:Header – optional<br/>security, transaction, routing...]
BD[soap:Body – mandatory<br/>operation + parameters<br/>OR soap:Fault]
end
end
| Element | Required? | Purpose |
|---|---|---|
| Envelope | Yes | The outer (root) element. It tells the receiver "this XML is a SOAP message" and declares the SOAP namespace. |
| Header | No | Extra information (metadata) for the receiver or for computers in between: login tokens, transaction IDs, routing. If present, it must be the first element inside the Envelope. |
| Body | Yes | The real content (payload): the request (which operation + its inputs) or the response. |
| Fault | No (inside Body) | Error details when something goes wrong. |
Example request
The client asks: "convert 37 °C to Fahrenheit". The XML travels inside an HTTP POST:
POST /ws/temperature HTTP/1.1
Host: localhost:8080
Content-Type: text/xml; charset=utf-8
SOAPAction: ""
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:tem="http://virtuallp.com/temperature">
<soap:Header>
<tem:AuthToken>eyJhbGciOiJIUzI1NiJ9...</tem:AuthToken>
</soap:Header>
<soap:Body>
<tem:celsiusToFahrenheit>
<celsius>37</celsius>
</tem:celsiusToFahrenheit>
</soap:Body>
</soap:Envelope>
- The Header carries a login token (
AuthToken). - The Body says which operation to run (
celsiusToFahrenheit) and the input (37).
Example response
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:celsiusToFahrenheitResponse xmlns:ns2="http://virtuallp.com/temperature">
<return>98.6</return>
</ns2:celsiusToFahrenheitResponse>
</soap:Body>
</soap:Envelope>
The answer 98.6 comes back inside the Body.
SOAP Fault
When something goes wrong, the Body carries a Fault instead of a normal answer, and the HTTP status is 500.
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<soap:Fault>
<faultcode>soap:Client</faultcode>
<faultstring>Temperature below absolute zero</faultstring>
<detail><ns2:InvalidTemperature xmlns:ns2="http://virtuallp.com/temperature"/></detail>
</soap:Fault>
</soap:Body>
</soap:Envelope>
faultcode– the type of error (here: the client sent bad data),faultstring– a human-readable message,detail– extra, application-specific details.
| SOAP 1.1 fault code | SOAP 1.2 equivalent | Meaning |
|---|---|---|
VersionMismatch |
VersionMismatch |
The Envelope namespace is wrong (wrong SOAP version) |
MustUnderstand |
MustUnderstand |
A header marked "must understand" was not understood |
Client |
Sender |
The request was wrong (bad data) – the caller's fault |
Server |
Receiver |
The server failed while handling a correct request |
SOAP 1.1 vs SOAP 1.2
| Aspect | SOAP 1.1 | SOAP 1.2 |
|---|---|---|
| Namespace | http://schemas.xmlsoap.org/soap/envelope/ |
http://www.w3.org/2003/05/soap-envelope |
| Content-Type | text/xml |
application/soap+xml |
| Action | SOAPAction HTTP header |
action parameter inside Content-Type |
| Fault codes | Client / Server | Sender / Receiver |
| Status | W3C (World Wide Web Consortium) Note | W3C Recommendation |
JAX-WS – SOAP web services in Java
JAX-WS (Java API (Application Programming Interface) for XML Web Services) helps Java programs create and call SOAP services. It uses annotations, such as @WebService, to mark service code. JAXB (Java Architecture for XML Binding) converts between Java objects and XML.
How a JAX-WS call works
sequenceDiagram
participant C as Client code
participant P as Proxy (stub)
participant R as JAX-WS runtime (client)
participant S as JAX-WS runtime (server)
participant I as Service implementation (SEI)
C->>P: tempService.celsiusToFahrenheit(37)
P->>R: marshal arguments (JAXB)
R->>S: SOAP Envelope over HTTP POST
S->>I: unmarshal + invoke method
I-->>S: return 98.6
S-->>R: SOAP response Envelope
R-->>P: unmarshal result
P-->>C: 98.6
- Your client code calls a normal-looking Java method.
- The proxy (stub) – generated code – turns the arguments into XML (marshalling).
- The XML travels as a SOAP Envelope over HTTP.
- The server turns the XML back into Java values (unmarshalling) and calls the real method.
- The result travels back the same way, and your code gets
98.6as if it were a local call.
Key annotations
| Annotation | Placed on | Purpose |
|---|---|---|
@WebService |
Class / interface | Marks a Service Endpoint Interface (SEI – the list of operations) or its implementation |
@WebMethod |
Method | Makes a method a web service operation (optional operationName) |
@WebParam |
Parameter | Gives the XML name for a parameter |
@WebResult |
Method | Gives the XML name for the return value |
@SOAPBinding |
Class | Chooses style = DOCUMENT/RPC, use = LITERAL |
@Oneway |
Method | A one-way operation (no response) |
@HandlerChain |
Class | Attaches SOAP handlers (next lesson) |
Step 1 – the service endpoint interface and implementation
The interface lists the operations:
package com.virtuallp.ws;
import jakarta.jws.WebMethod;
import jakarta.jws.WebParam;
import jakarta.jws.WebService;
import jakarta.jws.soap.SOAPBinding;
@WebService
@SOAPBinding(style = SOAPBinding.Style.DOCUMENT)
public interface Temperature {
@WebMethod
double celsiusToFahrenheit(@WebParam(name = "celsius") double celsius);
}
The implementation does the real work:
package com.virtuallp.ws;
import jakarta.jws.WebService;
@WebService(endpointInterface = "com.virtuallp.ws.Temperature",
serviceName = "TemperatureService")
public class TemperatureImpl implements Temperature {
@Override
public double celsiusToFahrenheit(double celsius) {
if (celsius < -273.15) {
throw new IllegalArgumentException("Temperature below absolute zero");
}
return celsius * 9 / 5 + 32;
}
}
Step 2 – publish the endpoint
package com.virtuallp.ws;
import jakarta.xml.ws.Endpoint;
public class TemperaturePublisher {
public static void main(String[] args) {
Endpoint.publish("http://localhost:8080/ws/temperature", new TemperatureImpl());
System.out.println("Service running – WSDL at http://localhost:8080/ws/temperature?wsdl");
}
}
Endpoint.publish starts a small built-in web server, creates the WSDL automatically and shows it at the ?wsdl address.
Step 3 – write a client
First generate the client code from the WSDL:
wsimport -keep -p com.virtuallp.client http://localhost:8080/ws/temperature?wsdl
Then use it like a normal Java object:
package com.virtuallp.client;
public class TemperatureClient {
public static void main(String[] args) {
TemperatureService service = new TemperatureService(); // generated
Temperature port = service.getTemperatureImplPort(); // generated proxy
System.out.println("37 °C = " + port.celsiusToFahrenheit(37) + " °F");
}
}
Output: 37 °C = 98.6 °F
JAXB – mapping Java to XML
JAX-WS uses JAXB to marshal (Java object → XML) and unmarshal (XML → Java object). Complex inputs and outputs are just ordinary Java classes (beans):
@XmlRootElement
public class Student {
private String rollNo;
private String name;
private double cgpa;
// getters and setters
}
A method Student findStudent(String rollNo) then automatically sends and receives a <student> XML element.
Advantages and disadvantages of SOAP
| Advantages | Disadvantages |
|---|---|
| Strict contracts (WSDL + XSD (XML Schema Definition)) – both sides know exactly what to send | Long XML messages – more data to send |
| Built-in standards for security, transactions and reliable delivery (WS-*) | Slower to read (parse) than JSON (JavaScript Object Notation) |
| Works over many transports, not only HTTP | Harder to learn and debug |
| Formal, standard error handling (Fault) | Not a good fit for browsers and mobile apps |
| Excellent tools that generate code | Mostly uses POST, so normal web caching cannot be used |
Virtual lab · Web Services · 30 min
Call a SOAP service and a REST API from a client
Import the TemperatureService WSDL into SoapUI, send a SOAP envelope and provoke a SOAP Fault, then use Postman to perform full CRUD on the Students REST API and compare status codes, headers and payloads.
Summary
- A SOAP message = Envelope (must) → Header (optional) → Body (must) with the content or a Fault.
- SOAP 1.2 uses
application/soap+xml, Sender/Receiver fault codes and a W3C-recommended namespace. - JAX-WS uses
@WebService,@WebMethod,@WebParam; JAXB converts objects to XML and back. Endpoint.publishstarts a service and creates its WSDL;wsimportcreates client stubs.
Practice questions
- What is SOAP? List its features.
- Explain the structure of a SOAP message with an example request and response.
- What is a SOAP Fault? Explain its sub-elements and fault codes.
- Compare SOAP 1.1 and SOAP 1.2.
- What is JAX-WS? Explain the annotations used to create a web service.
- Write the steps (with code) to create, publish and consume a JAX-WS web service.
- What are marshalling and unmarshalling? Which API performs them in JAX-WS?
Practise this topic
Hands-on virtual lab
Apply what you just read in a realistic, guided simulation. Free with a verified account.
Create a free account to track your progress and unlock virtual labs.