2

I have a Spring-WS web service where i would like to be able to send back binary data to the client. My intention is to format the SOAP response to either use MTOM or Soap with attachements. The response will be a JAXB object.

I have already setup the Spring-WS environment and can send and receive requests usign JAXB. I am however struggling to setup the Binary attachement response as most of the tutorials and information on the web are mostly related to Clients sending attachements whereas i am interested in Server sending attachements back to the client.

Two questions:

  • Which is the recommended approach for returning binary data to the client? SOAP with attachements, MTOM or just simply return the base64encoded data as an xml tag?

  • Are there any examples that shows how to configure Spring-WS to return Binary attachments using either of the two methods?

ziggy
  • 15,677
  • 67
  • 194
  • 287
  • could you please checkout,had doubt about whether mtom caches the entire data in memory before it sends https://stackoverflow.com/questions/61483726/does-using-mtom-on-client-load-entire-binary-data-in-client-memory-before-sendin. – yolob 21 May 06 '20 at 12:45

1 Answers1

2

1) Mtom is apparently the de-facto standard for sending files via soap, so i would recommend that.

2) if you look at the mtom example that comes with the spring-ws distrobution (not from maven) you will see 90% of the setup required. the one difference is the expectedContentType for your attachment you should set that to application/octet-stream

now your code on the server will look something like this

try {
        dataHandler = new DataHandler(new File("/tmp/test.exe").toURL());
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    return new JaxbObjectResponse("id", dataHandler);

and your schema definition looks something like this.

<xsd:element name="JaxbObjectResponse">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="id" type="xsd:string" />
            <xsd:element name="file" type="xsd:base64Binary" xmime:expectedContentTypes="application/octet-stream" />
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>
Shaun Stone
  • 626
  • 1
  • 5
  • 19
  • Yes i did eventually find the MTOM example in the spring-ws example. Do you think that if i use MTOM the client also needs to be MTOM enabled? I did get it to work but the problem i find is that when i test the service, some clients (SoapUI) return the correct response and some clients (XMLSpy) just spit out an error saying that the service returned a NULL response. Does the client have to support MTOM as well for me to be able to use MTOM? – ziggy Jul 13 '12 at 09:14
  • The client will have to understand mtom or more specifically xop. because the xop element will give the client the content-id for which multipart response maps to this element. xop is an official standard so all (good/proper) clients should be able to support it. http://www.w3.org/TR/xop10/ – Shaun Stone Jul 13 '12 at 17:32
  • Ok Thanks. I got the MTOM variant to work ok. I am now trying SAAJ (to be able to use attachments) but have yet to get it to work. http://stackoverflow.com/questions/11430527/spring-ws-web-service-adding-an-attachment-to-the-response-using-saaj-no-adap – ziggy Jul 13 '12 at 17:37