Return to site

🌐🧼 WEB SERVICES WITH JAX-WS (SOAP): THE ENTERPRISE WAY

· jakartaee

JAX-WS is SOAP web services in the Java ecosystem (Java EE / Jakarta EE). It’s still everywhere in “serious” enterprise stacks: contracts-first WSDLs, strong typing, and integrations with systems that aren’t moving to REST anytime soon. 😄

🔸 TLDR ✅

  1. ▪️ JAX-WS = SOAP in Java/Jakarta EE
  2. ▪️ Generate clients from WSDL for strong typing
  3. ▪️ Use MTOM for real-world file transfers 📎
  4. ▪️ EJB + JAX-WS is a powerful combo for transactional enterprise services

🔸 DEVELOPING WEBSERVICES WITH JAX-WS

  1. ▪️ Expose a SOAP endpoint with @WebService + @WebMethod
  2. ▪️ Generate a strongly-typed client from the WSDL (or build one dynamically)
  3. ▪️ Send attachments efficiently with MTOM (no more huge base64 payloads 🧱)

Endpoint (server-side)

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

@WebService(serviceName = "OrderService")
public class OrderEndpoint {

  @WebMethod
  public String placeOrder(String sku, int qty) {
    return "OK-" + sku + "-" + qty;
  }
}

Client (generated stub style)

// Generated from WSDL (tooling depends on your stack: Metro/CXF, etc.)
OrderService service = new OrderService();
OrderEndpoint port = service.getOrderEndpointPort();

String confirmation = port.placeOrder("SKU-42", 2);

🔸 SENDING ATTACHMENTS (MTOM) 📎

Without MTOM, attachments often end up as heavy base64 blobs in XML. With MTOM, the SOAP message stays lean and the binary goes as an optimized part.

Endpoint with MTOM + DataHandler

import jakarta.jws.WebService;
import jakarta.xml.ws.soap.MTOM;
import jakarta.activation.DataHandler;
import jakarta.xml.bind.annotation.XmlMimeType;

@MTOM
@WebService(serviceName = "DocService")
public class DocEndpoint {

  public String upload(
      String name,
      @XmlMimeType("application/pdf") DataHandler file
  ) {
    return "RECEIVED-" + name;
  }
}

Client call with a PDF

import jakarta.activation.DataHandler;
import jakarta.activation.FileDataSource;

DataHandler dh = new DataHandler(new FileDataSource("invoice.pdf"));
String result = docPort.upload("invoice.pdf", dh);

🔸 EXPOSING EJBS AS WEB SERVICES (EJB + JAX-WS) 🏛️

EJBs can be published as SOAP endpoints with the same @WebService model, while keeping EJB benefits (transactions, pooling, security, etc.).

Expose a Stateless EJB as a SOAP service

import jakarta.ejb.Stateless;
import jakarta.jws.WebService;
import jakarta.jws.WebMethod;

@Stateless
@WebService(serviceName = "CustomerService")
public class CustomerServiceBean {

  @WebMethod
  public String findCustomerName(long id) {
    return "Customer-" + id;
  }
}

🔸 EJB WEB SERVICE CLIENTS (CALL SOAP FROM AN EJB) 🔁

You can inject a generated service into an EJB and call it like any typed Java API.

import jakarta.ejb.Stateless;
import jakarta.xml.ws.WebServiceRef;

@Stateless
public class BillingBean {

  @WebServiceRef
  private CustomerService service; // generated from WSDL

  public String bill(long customerId) {
    var port = service.getCustomerServicePort();
    return "BILLING for " + port.findCustomerName(customerId);
  }
}

🔸 TAKEAWAYS 🎯

  1. ▪️ If you need a strict contract, tooling, and interoperability: SOAP/JAX-WS still delivers
  2. ▪️ MTOM is the “attachment mode” you want in production
  3. ▪️ EJB endpoints are great when you also want transactions + container services
  4. ▪️ Heads-up: in modern stacks this is typically Jakarta XML Web Services (package names changed from javax.* to jakarta.*)

#Java #JakartaEE #JAXWS #SOAP #WebServices #EJB #MTOM #EnterpriseJava #BackendDevelopment #Integration

Go further with Java certification:

Java👇

Spring👇

SpringBook👇

JavaBook👇