Building a SOAP Service with JAX-WS

Harry · 11 Sep 2026 · 14 views

Building a SOAP Service with JAX-WS

JAX-WS (Java API for XML Web Services) is the standard for building SOAP web services in Java. While REST dominates new development, SOAP services remain critical in enterprise environments.

Creating the Service Endpoint

Define your service using JAX-WS annotations. The @WebService annotation marks the class as a SOAP service endpoint.

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

@WebService(serviceName = "OrderService",
            targetNamespace = "http://example.com/orders")
public class OrderServiceImpl {

    private final Map<Long, Order> orders = new ConcurrentHashMap<>();

    @WebMethod(operationName = "createOrder")
    public Order createOrder(
            @WebParam(name = "productName") String productName,
            @WebParam(name = "quantity") int quantity) {
        long id = orders.size() + 1;
        Order order = new Order(id, productName, quantity, "PENDING");
        orders.put(id, order);
        return order;
    }

    @WebMethod(operationName = "getOrder")
    public Order getOrder(
            @WebParam(name = "orderId") long orderId) {
        return orders.get(orderId);
    }
}

Publishing the Service

Publish the endpoint using Endpoint. The service is now accessible at the specified URL.

import jakarta.xml.ws.Endpoint;

public class SoapPublisher {
    public static void main(String[] args) {
        Endpoint.publish(
            "http://localhost:8080/orders",
            new OrderServiceImpl()
        );
        System.out.println("SOAP service published at /orders?wsdl");
    }
}

Visit http://localhost:8080/orders?wsdl to see the WSDL - the XML contract that describes the service.

The JAXB Data Class

JAX-WS uses JAXB to convert Java objects to XML. The data class needs standard annotations:

import jakarta.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = {"id", "productName", "quantity", "status"})
public class Order {
    private long id;
    private String productName;
    private int quantity;
    private String status;

    // no-arg constructor required by JAXB
    public Order() {}
    public Order(long id, String name, int qty, String status) {
        this.id = id;
        this.productName = name;
        this.quantity = qty;
        this.status = status;
    }
}

Spring Boot + JAX-WS

Use the spring-boot-starter-web-services dependency and @Endpoint annotation to integrate JAX-WS with Spring Boot for dependency injection and configuration.

Key Points

  • @WebService marks a class as a SOAP service endpoint.
  • JAX-WS uses JAXB for XML marshalling/unmarshalling of Java objects.
  • Endpoint.publish() exposes the service at a URL.
  • The WSDL at ?wsdl is the XML contract clients use to generate code.
  • JAXB data classes need a no-arg constructor and proper annotations.
Share this post:

Comments (0)

Please login or register to comment.