Skip to content
VirtualLP Virtual Learning Platform

Practical 2: Develop and Deploy a SOAP Web Service

14 min read Topic 26 of 34 1 hands-on lab

Aim

Create a SOAP (originally Simple Object Access Protocol) service with two operations: convertCurrency() and getServiceStatus(). Open its WSDL (Web Services Description Language) description. Send test requests, read the replies and check what happens with wrong input.

Theory

SOAP is a set of rules for sending messages between programs. The messages use XML (Extensible Markup Language), a text format with tags.

Every SOAP message has an Envelope, the outer container, and a Body, which holds the data. An optional Header carries extra details. A Fault in the Body describes an error.

courier parcel

The Envelope is the box, the Header is the label on top (optional), the Body is what's inside, and a Fault is a "returned – wrong address" slip.

sequenceDiagram
    participant C as SOAP client
    participant T as Tomcat + JAX-WS runtime
    participant S as CurrencyConverter
    C->>T: GET /currency/CurrencyService?wsdl
    T-->>C: WSDL (types, messages, portType, binding, service)
    C->>T: POST SOAP Envelope <convertCurrency><amountInr>1000</amountInr>
    T->>S: convertCurrency(1000.0)
    S-->>T: 12.0
    T-->>C: 200 · <convertCurrencyResponse><return>12.0</return>
    C->>T: POST <amountInr>-50</amountInr>
    S--xT: IllegalArgumentException
    T-->>C: 500 · <S:Fault><faultstring>Amount must not be negative</faultstring>
  1. The client first downloads the WSDL (the menu card).
  2. It sends a SOAP envelope asking to convert 1000.
  3. Tomcat's JAX-WS (Java API for XML Web Services) runtime calls the Java method, which returns 12.0.
  4. The answer travels back in a SOAP envelope with HTTP (Hypertext Transfer Protocol) 200.
  5. When the client sends -50, the method throws an error, and the client gets a SOAP Fault with HTTP 500.

Structure of the WSDL

Element Describes
types The XML Schema of the request/response data (amountInr is a number – xs:double)
message The input and output message of each operation
portType The abstract interface – the list of operations
binding How messages travel – SOAP 1.1, document/literal style
service / port The real address of the service (soap:address location=…)

JAX-WS annotations

Annotation Purpose
@WebService(serviceName = "CurrencyService") Makes the class a web service and gives the service its name
@WebMethod Makes a method an operation that clients can call
@WebParam(name = "amountInr") Gives the parameter's name in the WSDL and the SOAP body
Key point

Wrong input is reported with a SOAP Fault (HTTP 500), which has a faultcode (Client or Server) and a faultstring (the message). Wrong types, such as abc for a number, are rejected by the runtime while it reads the XML (unmarshalling) – before your code even runs.

Requirements

Use a Jakarta-compatible Metro runtime with Tomcat 10.1. Java 11+ does not include JAX-WS or wsimport; install these tools separately. Keep the runtime, generated client and jakarta.* imports compatible. JDK (Java Development Kit) 21, Eclipse IDE (Integrated Development Environment) for Enterprise Java, Apache Tomcat 10.1 with JAX-WS (Metro), a SOAP client (SoapUI or Eclipse Web Services Explorer) and curl.

Program – CurrencyConverter.java

package com.vlp.ws;

import jakarta.jws.WebMethod;
import jakarta.jws.WebParam;
import jakarta.jws.WebService;

@WebService(serviceName = "CurrencyService")
public class CurrencyConverter {

    private double exchangeRate = 0.012;

    @WebMethod
    public double convertCurrency(@WebParam(name = "amountInr") double amountInr) {
        if (amountInr < 0) {
            throw new IllegalArgumentException("Amount must not be negative");
        }
        return Math.round(amountInr * exchangeRate * 100.0) / 100.0;
    }

    @WebMethod
    public String getServiceStatus() {
        return "UP - 1 INR = " + exchangeRate + " USD";
    }
}

What the code does:

  • convertCurrency – refuses negative amounts (throws an error → SOAP Fault), otherwise multiplies by 0.012 and rounds to 2 decimals.
  • getServiceStatus – returns "UP" and the current rate, so you can quickly check the service is alive.

Procedure

  1. In Eclipse, create a Dynamic Web Project and add the class above.
  2. Right-click → Run As → Run on Server (Tomcat).
  3. Open http://localhost:8080/currency/CurrencyService?wsdl and find each WSDL part: types, message, portType, binding, service.
  4. Load this WSDL into the SOAP client (SoapUI or Web Services Explorer).
  5. Choose convertCurrency, set amountInr = 1000 and click invoke – you should get 12.0.
  6. Invoke getServiceStatus – you should get UP - 1 INR = 0.012 USD.
  7. Send -50 – observe the SOAP Fault "Amount must not be negative".
  8. Send abc – observe the SOAP Fault from the runtime (wrong type).
  9. Check the endpoint is reachable: curl -i "http://localhost:8080/currency/CurrencyService?wsdl" → it should show HTTP/1.1 200.

Virtual lab · Web Services · 30 min

Practical 2: Develop, deploy and test a SOAP web service

Write a JAX-WS service with two operations, publish its WSDL on Tomcat, call it with SOAP request envelopes, provoke a SOAP fault with invalid input and verify the endpoint with curl.

Open lab

Output / Observations

Example output for the settings in this lesson:

Request

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.vlp.com/">
   <soapenv:Header/>
   <soapenv:Body>
      <ws:convertCurrency>
         <amountInr>1000</amountInr>
      </ws:convertCurrency>
   </soapenv:Body>
</soapenv:Envelope>

Response

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns2:convertCurrencyResponse xmlns:ns2="http://ws.vlp.com/">
         <return>12.0</return>
      </ns2:convertCurrencyResponse>
   </S:Body>
</S:Envelope>

Fault for -50: <faultcode>S:Server</faultcode><faultstring>Amount must not be negative</faultstring>

  • 1000 × 0.012 = 12.0 ✔
  • The request has an empty Header and the operation inside the Body; the response has the result in <return>.

Conclusion

Check that the service returns a converted amount and a status reply. Wrong input should return a SOAP Fault. Open the WSDL and use curl to check that the service address can be reached.

Viva questions

  1. Name the parts of a SOAP message. Envelope, Header (optional), Body, and Fault (inside the Body for errors).
  2. What is the difference between portType and binding? portType is the abstract list of operations; binding says how they travel (SOAP 1.1, document/literal, HTTP).
  3. Which HTTP status comes with a SOAP fault? 500 Internal Server Error.
  4. What is document/literal style? The Body contains an XML document that is checked against the schema in types.
  5. How do you view a service's WSDL? Add ?wsdl to the end of the endpoint URL (Uniform Resource Locator).
  6. When is faultcode Client vs Server? Client – the request was wrong (for example could not be read); Server – processing failed inside the service.

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.