Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The documentation in Spring-WS is particularly light on this topic, and it's actually pretty easy to add a SOAP attachment. I'll make a few assumptions:</p> <ol> <li>Your WSDL correctly specifies the <code>mime:multipartRelated</code> on the output message</li> <li>We're going to use the Saaj SOAP message factory</li> </ol> <p>Attachments reside in the MimeContainer on the SOAP message. To get this container, we need to manually construct the SOAP response, well, just part of it. Doing that looks like this:</p> <pre><code>SaajSoapMessageFactory factory = new SaajSoapMessageFactory( javax.xml.soap.MessageFactory.newInstance()); SaajSoapMessage message = factory.createWebServiceMessage(); </code></pre> <p>Now we need a DataHandler for our picture:</p> <pre><code>DataHandler handler = new DataHandler( new URLDataSource( Service.class.getResource("coffee.jpg"))); message.addAttachment("picture", handler); </code></pre> <p>Okay, content: check, message: check, hm, still need to get the response to go out. The tricky part here is that we need to bring in the <code>MessageContext</code> so that we can set <em>this</em> particular message to be the one we respond with, we do that by editing our definition of <code>getCoffee</code>:</p> <pre><code>public Coffee getCoffee(@RequestPayload Order order, MessageContext context) </code></pre> <p><strong>Putting it all together:</strong></p> <pre><code>@Endpoint public class Service{ private SaajSoapMessageFactory saajMessageFactory; // dependency inject this @PayloadRoot(namespace = "http://foo.com/coffee", localPart = "order") @ResponsePayload public Coffee getCoffee(@RequestPayload Order order, MessageContext context){ DataHandler handler = new DataHandler( new URLDataSource( Service.class.getResource("coffee.jpg"))); SaajSoapMessage message = saajMessageFactory.createWebServiceMessage(); message.addAttachment("picture", handler); context.setResponse(message); return new Coffee("Hot Joe"); } public void setSaajMessageFactory(SaajMessageFactory saajMessageFactory){ this.saajMessageFactory = saajMessageFactory; } public SaajMessageFactory getSaajMessageFactory(){ return saajMessageFactory; } } </code></pre> <p>For good measure, here's the beans dependency injection for getting a <code>SaajMessageFactory</code>:</p> <pre class="lang-xml prettyprint-override"><code>&lt;bean id="soapMessageFactory" class="javax.xml.soap.MessageFactory" factory-method="newInstance" /&gt; &lt;bean id="saajMessageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory"&gt; &lt;constructor-arg ref="soapMessageFactory" /&gt; &lt;/bean&gt; &lt;bean id="myService" class="Service"&gt; &lt;property name="saajMessageFactory" ref="saajMessageFactory" /&gt; &lt;/bean&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload