Creating and Sending a Simple Message
Creating and Sending a Simple Message
Creating and Sending a Simple Message
This section covers the basics of creating and sending a simple message and retrieving the
content of the response. It includes the following topics:
Creating a Message
The first step is to create a message using a MessageFactory object. The SAAJ API provides a
default implementation of the MessageFactory class, thus making it easy to get an instance.
The following code fragment illustrates getting an instance of the default message factory and
then using it to create a message.
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory.createMessage();
As is true of the newInstance method for SOAPConnectionFactory, the newInstance method
for MessageFactory is static, so you invoke it by calling MessageFactory.newInstance.
If you specify no arguments to the newInstance method, it creates a message factory for SOAP
1.1 messages. To create a message factory that allows you to create and process SOAP 1.2
messages, use the following method call:
MessageFactory factory =
MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
To create a message factory that can create either SOAP 1.1 or SOAP 1.2 messages, use the
following method call:
MessageFactory factory =
MessageFactory.newInstance(SOAPConstants.DYNAMIC_SOAP_PROTOCOL);
This kind of factory enables you to process an incoming message that might be of either type.
Parts of a Message
A SOAPMessage object is required to have certain elements, and, as stated previously, the SAAJ
API simplifies things for you by returning a new SOAPMessage object that already contains these
elements. When you call createMessage with no arguments, the message that is created
automatically has the following:
SAAJ Tutorial
Chapter 19 · SOAP with Attachments API for Java
591