📄 saaj2.html
字号:
</p><p> <a name="63962"> </a><strong><font >Figure 9-1 <code class="cCode">SOAPMessage</code> Object with No Attachments</font></strong></p><a name="wp73089"> </a><h5 class="pHeading4">Messages with Attachments</h5><a name="wp73418"> </a><p class="pBody">A SOAP message may include one or more attachment parts in addition to the SOAP part. The SOAP part may contain only XML content; as a result, if any of the content of a message is not in XML format, it must occur in an attachment part. So, if for example, you want your message to contain a binary file, your message must have an attachment part for it. Note that an attachment part can contain any kind of content, so it can contain data in XML format as well. <a href="SAAJ2.html#wp74443">Figure 9-2</a> shows the high-level structure of a SOAP message that has two attachments.</p><a name="wp74446"> </a><p class="pBody"></p><div align="left"><img src="images/Fig9-22.gif" height="428" width="286" alt="SOAPMessage Object with Two AttachmentPart Objects" border="0" hspace="0" vspace="0"/></div><p class="pBody"></p><p> <a name="74443"> </a><strong><font >Figure 9-2 <code class="cCode">SOAPMessage</code> Object with Two <code class="cCode">AttachmentPart</code> Objects</font></strong></p><a name="wp74416"> </a><p class="pBody">The SAAJ API provides the <code class="cCode">AttachmentPart</code> class to represent the attachment part of a SOAP message. A <code class="cCode">SOAPMessage</code> object automatically has a <code class="cCode">SOAPPart</code> object and its required subelements, but because <code class="cCode">AttachmentPart</code> objects are optional, you have to create and add them yourself. The tutorial section will walk you through creating and populating messages with and without attachment parts.</p><a name="wp73215"> </a><p class="pBody">If a <code class="cCode">SOAPMessage</code> object has one or more attachments, each <code class="cCode">AttachmentPart</code> object must have a MIME header to indicate the type of data it contains. It may also have additional MIME headers to identify it or to give its location, which are optional but can be useful when there are multiple attachments. When a <code class="cCode">SOAPMessage</code> object has one or more <code class="cCode">AttachmentPart</code> objects, its <code class="cCode">SOAPPart</code> object may or may not contain message content. </p><a name="wp80777"> </a><h4 class="pHeading3">SAAJ and DOM</h4><a name="wp80778"> </a><p class="pBody">At SAAJ 1.2, the SAAJ APIs extend their counterparts in the <code class="cCode">org.w3c.dom</code> package: </p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp84956"> </a><div class="pSmartList1"><li>The <code class="cCode">Node</code> interface extends the <code class="cCode">org.w3c.dom.Node</code> interface. </li></div><a name="wp84957"> </a><div class="pSmartList1"><li>The <code class="cCode">SOAPElement</code> interface extends both the <code class="cCode">Node</code> interface and the <code class="cCode">org.w3c.dom.Element</code> interface.</li></div><a name="wp84961"> </a><div class="pSmartList1"><li>The <code class="cCode">SOAPPart</code> class implements the <code class="cCode">org.w3c.dom.Document</code> interface.</li></div><a name="wp84959"> </a><div class="pSmartList1"><li>The <code class="cCode">Text</code> interface extends the <code class="cCode">org.w3c.dom.Text</code> interface.</li></div></ul></div><a name="wp84958"> </a><p class="pBody">Moreover, the <code class="cCode">SOAPPart</code> of a <code class="cCode">SOAPMessage</code> is also a DOM Level 2 <code class="cCode">Document</code>, and can be manipulated as such by applications, tools and libraries that use DOM. See Chapter <a href="JAXPDOM.html#wp79996">6</a> for details about DOM. See <a href="SAAJ3.html#wp64119">Adding Content to the SOAPPart Object</a> and <a href="SAAJ3.html#wp78963">Adding a Document to the SOAP Body</a> for details on how to use DOM documents with the SAAJ API.</p><a name="wp63975"> </a><h3 class="pHeading2">Connections</h3><a name="wp63976"> </a><p class="pBody">All SOAP messages are sent and received over a connection. With the SAAJ API, the connection is represented by a <code class="cCode">SOAPConnection</code> object, which goes from the sender directly to its destination. This kind of connection is called a <em class="cEmphasis">point-to-point</em> connection because it goes from one endpoint to another endpoint. Messages sent using the SAAJ API are called <em class="cEmphasis">request-response messages</em>. They<em class="cEmphasis"> </em>are sent over a <code class="cCode">SOAPConnection</code> object with the method <code class="cCode">call</code>, which sends a message (a request) and then blocks until it receives the reply (a response). </p><a name="wp73652"> </a><h4 class="pHeading3">SOAPConnection Objects</h4><a name="wp63982"> </a><p class="pBody">The following code fragment creates the <code class="cCode">SOAPConnection</code> object <code class="cCode">connection</code>, and then, after creating and populating the message, uses <code class="cCode">connection</code> to send the message. As stated previously, all messages sent over a <code class="cCode">SOAPConnection</code> object are sent with the method <code class="cCode">call</code>, which both sends the message and blocks until it receives the response. Thus, the return value for the method <code class="cCode">call</code> is the <code class="cCode">SOAPMessage</code> object that is the response to the message that was sent. The parameter <code class="cCode">request</code> is the message being sent; <code class="cCode">endpoint</code> represents where it is being sent. </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><code class="cCode">SOAPConnectionFactory factory = SOAPConnectionFactory.newInstance();SOAPConnection connection = factory.createConnection();</code>. . .// create a request message and give it contentjava.net.URL endpoint = new URL("http://fabulous.com/gizmo/order");SOAPMessage response = connection.call(request, endpoint);<a name="wp63983"> </a></pre></div><a name="wp63985"> </a><p class="pBody">Note that the second argument to the method <code class="cCode">call</code>, which identifies where the message is being sent, can be a <code class="cCode">String</code> object or a <code class="cCode">URL</code> object. Thus, the last two lines of code from the preceding example could also have been the following:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">String endpoint = "http://fabulous.com/gizmo/order";SOAPMessage response = connection.call(request, endpoint);<a name="wp76437"> </a></pre></div><a name="wp76423"> </a><p class="pBody">A Web service implemented for request-response messaging must return a response to any message it receives. The response is a <code class="cCode">SOAPMessage</code> object, just as the request is a <code class="cCode">SOAPMessage</code> object. When the request message is an update, the response is an acknowledgement that the update was received. Such an acknowledgement implies that the update was successful. Some messages may not require any response at all. The service that gets such a message is still required to send back a response because one is needed to unblock the <code class="cCode">call</code> method. In this case, the response is not related to the content of the message; it is simply a message to unblock the <code class="cCode">call</code> method.</p><a name="wp75236"> </a><p class="pBody">Now that you have some background on SOAP messages and SOAP connections, in the next section you will see how to use the SAAJ API.</p> </blockquote> <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider"> <table width="550" summary="layout" id="SummaryNotReq1"> <tr> <td align="left" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/download.html#tutorial" target="_blank">Download</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/faq.html" target="_blank">FAQ</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/history.html" target="_blank">History</a> </td> <td align="center" valign="center"><a accesskey="p" href="SAAJ.html"><img id="LongDescNotReq1" src="images/PrevArrow.gif" width="26" height="26" border="0" alt="Prev" /></a><a accesskey="c" href="J2EETutorialFront.html"><img id="LongDescNotReq1" src="images/UpArrow.gif" width="26" height="26" border="0" alt="Home" /></a><a accesskey="n" href="SAAJ3.html"><img id="LongDescNotReq3" src="images/NextArrow.gif" width="26" height="26" border="0" alt="Next" /></a><a accesskey="i" href="J2EETutorialIX.html"></a> </td> <td align="right" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/docs/api/index.html" target="_blank">API</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/search.html" target="_blank">Search</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/sendusmail.html" target="_blank">Feedback</a></font> </font> </td> </tr> </table> <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider"><p><font size="-1">All of the material in <em>The J2EE(TM) 1.4 Tutorial</em> is <a href="J2EETutorialFront2.html">copyright</a>-protected and may not be published in other workswithout express written permission from Sun Microsystems.</font> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -