background image

Code Examples

<< SOAPMessage Object | Header Example >>
<< SOAPMessage Object | Header Example >>

Code Examples

In summary, you have seen how to add a SOAPFault object and its contents to a message as well
as how to retrieve the contents. A SOAPFault object, which is optional, is added to the SOAPBody
object to convey status or error information. It must always have a fault code and a String
explanation of the fault. A SOAPFault object must indicate the actor that is the source of the
fault only when there are multiple actors; otherwise, it is optional. Similarly, the SOAPFault
object must contain a Detail object with one or more DetailEntry objects only when the
contents of the SOAPBody object could not be processed successfully.
See
"SOAP Fault Example" on page 624
for an example that uses code like that shown in this
section.
Code Examples
The first part of this tutorial uses code fragments to walk you through the fundamentals of using
the SAAJ API. In this section, you will use some of those code fragments to create applications.
First, you will see the program Request.java. Then you will see how to run the programs
HeaderExample.java
, DOMExample.java, DOMSrcExample.java, Attachments.java, and
SOAPFaultTest.java
.
Note ­
Before you run any of the examples, follow the preliminary setup instructions in
"Building the Examples" on page 70
.
Request Example
The class Request puts together the code fragments used in the section
"SAAJ Tutorial" on
page 590
and adds what is needed to make it a complete example of a client sending a
request-response message. In addition to putting all the code together, it adds import
statements, a main method, and a try/catch block with exception handling.
import javax.xml.soap.*;
import javax.xml.namespace.QName;
import java.util.Iterator;
import java.net.URL;
public class Request {
public static void main(String[] args)
{
try {
SOAPConnectionFactory soapConnectionFactory =
SOAPConnectionFactory.newInstance();
SOAPConnection connection =
soapConnectionFactory.createConnection();
Code Examples
Chapter 19 · SOAP with Attachments API for Java
613