Show List
Introduction To SOAP Web Services
A SOAP (Simple Object Access Protocol) web service is a method of communication between two systems over a network. It uses the standard HTTP protocol for sending and receiving messages, and uses XML as the format for the messages
The SOAP message are typically sent over HTTP, but can also be sent over other transport protocols such as SMTP. The SOAP message is wrapped in an HTTP request, with the SOAP message in the body of the request, and the HTTP headers used to provide additional information such as authentication details.
Here is a sample SOAP message that a client can send to a SOAP service that provides the weather of a city
<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<soap:Body xmlns:m="http://www.example.com/weather">
<m:GetWeather>
<m:Location>New York City</m:Location>
</m:GetWeather>
</soap:Body>
</soap:Envelope>
And here is an example of the SOAP message that the server might send back in response, containing the weather for the specified location:
<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<soap:Body xmlns:m="http://www.example.com/weather">
<m:WeatherResponse>
<m:Temperature>72</m:Temperature>
<m:Conditions>Sunny</m:Conditions>
</m:WeatherResponse>
</soap:Body>
</soap:Envelope>
A SOAP message has several key fields that are used to convey the information and structure of the message:
- Envelope: The root element of a SOAP message. It defines the start and end of the message and contains the other elements of the message.
- Header: Contains optional information that may be needed to process the message, such as authentication and routing information.
- Body: Contains the actual message, which is the payload of the SOAP message. The body element holds the contents of the message, such as the request or response data.
- Fault: This is an optional element that can be used to indicate an error has occurred. It includes details about the error, such as a code and a description.
- EncodingStyle: An attribute of the Envelope element that specifies the encoding style used for the contents of the SOAP message. The default encoding style is XML.
- xmlns: This is a namespace attribute that is used to indicate the namespace of the elements in the SOAP message. This allows different elements to have the same name but be in different namespaces, and also allows for versioning of SOAP messages.
- Additional elements: The SOAP message can contain other elements, depending on the specific use case. For example, a weather service might include elements for the location, temperature, and conditions in the Body element of the message.
SOAP Web Service Building Blocks
SOAP web service typically consists of the following components
- Web service provider: This is the system or application that provides the web service. It can be any system that can process SOAP messages, such as a server running a web service application, a database, or a legacy system.
- Web service: This is the actual service that is provided by the provider. It defines the operations (or methods) that can be performed, the input and output parameters, and the processing logic.
- Web service requestor (or client): This is the system or application that makes requests to the web service. It can be any system that can send SOAP messages, such as a mobile app, a website, or another server.
- SOAP message: This is the message format used for communication between the requestor and the service. It consists of an Envelope element that contains a Header and a Body element.
- WSDL: WSDL (Web Services Description Language) is an XML-based language that is used to describe the functionality offered by a web service. It defines the operations that a web service can perform, the input and output parameters for each operation, and the location of the service.
- Service Endpoint Interface (SEI): SEI is a Java interface that defines the operations that a web service can perform. It is used in JAX-WS to define the methods that a web service will expose to clients.
- Service Endpoint Implementation (SEI): SEI is a Java class that implements the operations defined in the SEI. It is responsible for handling the incoming SOAP messages and processing the request.
- Transport protocol: This is the protocol used to send the SOAP message between the requestor and the service. The most common transport protocol is HTTP, but other protocols such as SMTP can also be used.
- Protocol binding: This defines how the SOAP message is sent over the transport protocol. The most common binding is SOAP over HTTP, but other bindings such as SOAP over SMTP are also possible.
- Network: This is the physical or logical communication channel that connects the requestor and the service. It can be a local network, a wide area network (WAN), or the Internet.
- XML parser: The XML parser is needed to process the XML data in the SOAP message. It is used by both the client and the service to parse the SOAP message for further processing
- Namespaces: Namespaces are used to indicate the namespace of the elements in the SOAP service.
SOAP WSDL
WSDL (Web Services Description Language) is an XML-based language that is used to describe the functionality offered by a web service. It is used to define the operations that a web service can perform, the input and output parameters for each operation, and the location of the service.
WSDL is typically used in the following way:
- A web service provider creates a WSDL document that describes the web service. The WSDL document includes information such as the service's name, the location of the service, and the operations that the service can perform.
- The WSDL document is made available to web service requestors (or clients) who want to use the service.
- The client can use a tool or library to parse the WSDL document and generate code that can be used to access the web service. This generated code takes care of the details of sending and receiving SOAP messages, and allows the client to focus on the business logic of using the service.
- The client can use the generated code to access the web service by calling the methods defined in the WSDL document.
In summary, WSDL is used as a contract between the web service provider and the client, it describes the functionality offered by the service, and it allows clients to automatically generate code to access the service. This way the client doesn't need to know the details of the service, it just needs to use the generated code to access the service.
Here is a sample WSDL for the weather SOAP service. Explanation of each tag follows:
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://example.com/weather"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<wsdl:types>
<xsd:schema>
<xsd:import namespace="http://example.com/weather"
schemaLocation="weather.xsd"/>
</xsd:schema>
</wsdl:types>
<wsdl:message name="GetWeatherRequest">
<wsdl:part name="location" type="xsd:string"/>
</wsdl:message>
<wsdl:message name="GetWeatherResponse">
<wsdl:part name="response" type="tns:weatherReport"/>
</wsdl:message>
<wsdl:portType name="WeatherPort">
<wsdl:operation name="getWeather">
<wsdl:input message="tns:GetWeatherRequest"/>
<wsdl:output message="tns:GetWeatherResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="WeatherBinding" type="tns:WeatherPort">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="getWeather">
<soap:operation soapAction="http://example.com/weather/getWeather"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="WeatherService">
<wsdl:port name="WeatherServicePort" binding="tns:WeatherServiceSoapBinding">
<soap:address location="http://www.example.org/example/weather"/>
</wsdl:port> Here's an explanation of the main tags in the WSDL document:
- wsdl:definitions: This is the root element of the WSDL document. It defines the name, the namespace, etc. of the service.
- wsdl:types: It defines the data types used by the web service. WSDL uses XSD (XML Schema Definition) as the type system which helps with interoperability.
- xsd:import: This element imports the XSD schema that defines the structure of the weather report element.
- wsdl:message: This element defines the input and output messages for the web service operations. In this case, it defines two messages, one for the request and one for the response.
- wsdl:part: This element defines the individual parts of a message, it specifies the name and data type of the part.
- wsdl:portType: This element defines the web service operations, it specifies the name of the operation and input and output messages. In this case, it defines a single operation called "getWeather"
- wsdl:operation: This element defines an operation of a portType, it specifies the name of the operation, the input message and the output message.
- wsdl:binding: This element defines the protocol and data format used by the web service. In this case, it defines a binding called "WeatherBinding" that uses SOAP over HTTP.
- soap:binding: This element specifies the SOAP binding style (rpc in this case) and the transport protocol (http)
- soap:operation: This element specifies the SOAP action for the operation
- soap:body: this element specifies how the message body will be encoded, in this case it's using a literal encoding
XSD in SOAP Services
XSD (XML Schema Definition) is used in SOAP web services to define the structure of the SOAP messages that are exchanged between the client and the service. A SOAP message is an XML document, and an XSD schema can be used to define the structure of the elements in the SOAP message.
In a SOAP web service, the XSD schema is typically used in the following way:
- A web service provider creates an XSD schema that defines the structure of the SOAP messages that the service will accept. This schema defines the elements that can appear in the SOAP message, the data types of the elements, and the relationships between the elements.
- The XSD schema is included as part of the WSDL (Web Services Description Language) document that describes the web service. The WSDL document is used by clients to generate code for accessing the web service.
- When a client sends a SOAP message to the service, it first validates the message against the XSD schema to ensure that it conforms to the structure defined in the schema.
- When the service receives a SOAP message, it also validates the message against the XSD schema to ensure that it conforms to the expected structure.
By using an XSD schema, the web service can ensure that the SOAP messages it receives are well-formed and that they conform to the structure that the service is expecting. This can help to reduce the likelihood of errors and improve the reliability of the web service.
Here is a sample XSD document for the weather service report:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="weatherReport">
<xs:complexType>
<xs:sequence>
<xs:element name="location" type="xs:string"/>
<xs:element name="temperature" type="xs:decimal"/>
<xs:element name="condition" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Here's an explanation of the main tags
- xs:schema: This is the root element of the XSD schema, it defines the namespace and the target namespace of the schema.
- xs:element: This element defines an element that can appear in the XML document. In this case, it defines a single element called "weatherReport"
- xs:complexType: This element defines a complex type that can contain other elements. In this case, it defines a type for the "weatherReport" element.
- xs:sequence: This element defines a sequence of elements that must appear in the specified order. In this case, it defines a sequence of three elements, "location", "temperature", and "condition"
- xs:element: This element defines the child elements of the weatherReport element. It specifies the name of the element and its data type.
- xs:string, xs:decimal: These elements define the data type for the child elements, in this case string and decimal respectively
Leave a Comment