Java Reference
In-Depth Information
Creating a Complete SOAP Message
Problem
You want to create a complete SOAP request programmatically, and you don't have a com-
plete SOAP envelope in a physical file you can use to send your SAAJ request.
Solution
Use the javax.xml.ws.soap.SOAPMessage class to construct an envelope, any headers, the
body, and any attachments you need.
SOAPMessage is part of the SAAJ 1.3 API built-in to Java SE 6, so you only need to build a
SOAP message manually like this if you have not generated objects based on the WSDL.
SAAJ is defined by the contents of the javax.xml.ws.soap package, which gives you com-
plete, low-level programmatic control over the contents of your SOAP messages. Instead of
working with an object view of a service, SAAJ gives you an XML view.
Using SAAJ, you can do the following:
▪ Create a SOAP message
▪ Extract the content from the body of a SOAP message
▪ Create attachments for a SOAP message
▪ Create and extract content from SOAP headers
▪ Work with SOAP faults (exceptions)
▪ Send a SOAP message
The following code listing shows how to build a simple message whose body wraps a basic
string. You'll use this as the ISBN number to pass to the getBook method of your web service.
As you know, a SOAP message consists of an envelope, which is the single outer wrapper
type. SOAP 1.1 is represented by the http://schemas.xmlsoap.org/soap/envelope/ namespace
and defines an optional set of headers similar to HTTP headers, an optional fault child, an op-
tional set of attachments, and one mandatory element: body .
The SOAP 1.1 message you will build with Java will represent the following text under the
hood:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/
envelope/">
Search WWH ::




Custom Search