Simple Object Access Protocol and Web Services Description Language
Let's dive into how SOAP (Simple Object Access Protocol) and WSDL (Web Services Description Language) work, along with code examples.
SOAP (Simple Object Access Protocol):
SOAP is an XML-based protocol for exchanging structured data between systems, typically over HTTP. It defines a standard message format that consists of: 1. An Envelope (required)
2. A Header (optional)
3. A Body (required) Here's a simple SOAP message example: ```xml
<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
<soap:Header>
<!-- Optional header information -->
</soap:Header>
<soap:Body>
<m:GetStockPrice xmlns:m="http://www.example.org/stock">
<m:StockName>IBM</m:StockName>
</m:GetStockPrice>
</soap:Body>
</soap:Envelope>
``` WSDL (Web Services Description Language):
WSDL is an XML-based language used to describe web services. It defines: 1. The operations a web service offers
2. The format of the messages it accepts and returns
3. The protocols it supports
4. Where to find the service Here's a simplified WSDL example: ```xml
<?xml version="1.0"?>
<definitions name="StockQuoteService"
targetNamespace="http://example.com/stockquote.wsdl"
xmlns:tns="http://example.com/stockquote.wsdl"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns="http://schemas.xmlsoap.org/wsdl/"> <types>
<xsd:schema targetNamespace="http://example.com/stockquote.xsd">
<xsd:element name="TradePriceRequest">
<xsd:complexType>
<xsd:all>
<xsd:element name="tickerSymbol" type="xsd:string"/>
</xsd:all>
</xsd:complexType>
</xsd:element>
<xsd:element name="TradePrice">
<xsd:complexType>
<xsd:all>
<xsd:element name="price" type="xsd:float"/>
</xsd:all>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</types> <message name="GetLastTradePriceInput">
<part name="body" element="tns:TradePriceRequest"/>
</message>
<message name="GetLastTradePriceOutput">
<part name="body" element="tns:TradePrice"/>
</message> <portType name="StockQuotePortType">
<operation name="GetLastTradePrice">
<input message="tns:GetLastTradePriceInput"/>
<output message="tns:GetLastTradePriceOutput"/>
</operation>
</portType> <binding name="StockQuoteSoapBinding" type="tns:StockQuotePortType">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="GetLastTradePrice">
<soap:operation soapAction="http://example.com/GetLastTradePrice"/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding> <service name="StockQuoteService">
<port name="StockQuotePort" binding="tns:StockQuoteSoapBinding">
<soap:address location="http://example.com/stockquote"/>
</port>
</service> Now, let's see how we can implement this using JAX-WS in Java: 1. Define the service interface: ```java
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam; @WebService
public interface StockQuoteService {
@WebMethod
public float getLastTradePrice(@WebParam(name = "tickerSymbol") String symbol);
}
``` 2. Implement the service: ```java
import javax.jws.WebService; @WebService(endpointInterface = "StockQuoteService")
public class StockQuoteServiceImpl implements StockQuoteService {
public float getLastTradePrice(String symbol) {
// In a real implementation, this would fetch the actual stock price
if ("IBM".equals(symbol)) {
return 55.6f;
}
return 0;
}
}
``` 3. Publish the web service: ```java
import javax.xml.ws.Endpoint; public class StockQuotePublisher {
public static void main(String[] args) {
Endpoint.publish("http://localhost:9999/ws/stock", new StockQuoteServiceImpl());
System.out.println("Stock Quote Service is published!");
}
}
``` 4. Create a client to consume the web service: ```java
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import java.net.URL; public class StockQuoteClient {
public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:9999/ws/stock?wsdl");
QName qname = new QName("http://impl.service.example.com/", "StockQuoteServiceImplService");
Service service = Service.create(url, qname);
StockQuoteService stockQuote = service.getPort(StockQuoteService.class);
System.out.println("IBM stock price: " + stockQuote.getLastTradePrice("IBM"));
}
}
``` 1. JAX-WS uses the @WebService and @WebMethod annotations to generate the WSDL and SOAP binding automatically.
2. The SOAP messages are created and parsed by the JAX-WS runtime, abstracting these details from the developer.
3. The client uses the generated WSDL to create a proxy for the web service, allowing method calls as if they were local. This demonstrates how SOAP and WSDL work together in a JAX-WS environment, providing a standard way to describe and interact with web services across different platforms and programming languages.