O'Reilly logo

Java SOA Cookbook by Eben Hewitt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating a Complete SOAP Message

Problem

You want to create a complete SOAP request programmatically, and you don’t have a complete 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 complete, 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 optional set of attachments, and one mandatory element: body ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required