title: Spring-WS webservice with MTOM attachment - Hello world test
body: <p>I am trying to create a simple Spring webservice which when called returns a file attachment as part of the SOAP response. The Enpoint class is shown below: </p>
  
  <p>And finally the endpoint</p>
  
  <pre><code>@PayloadRoot(namespace="http://ws.mypackage.com", localPart="downloadMessageRequest")
      @ResponsePayload
      public JAXBElement&lt;DownloadResponseType&gt; invoke(@RequestPayload DownloadMessageRequest req) throws Exception  {
  
          DownloadResponseType response = new DownloadResponseType();
          DownloadResponseType.PayLoad payload = new DownloadResponseType.PayLoad();          
  
          javax.activation.DataHandler dataHandler = new javax.activation.DataHandler(new FileDataSource("c:\\temp\\maven-feather.png"));
          payload.setMessagePayLoad(dataHandler);
          response.setPayLoad(payload);
  
          return objectFactory.createDownloadMessageResponse(response);
  
      }
  </code></pre>
  
  <p>I would like the response to include the file as an attachement similar to the following response:</p>
  
  <pre><code>Content-Type: multipart/related; boundary=MIMEBoundary4A7AE55984E7438034;
                           type="application/xop+xml"; start="&lt;0.09BC7F4BE2E4D3EF1B@apache.org&gt;";
                           start-info="text/xml; charset=utf-8"
  
  --MIMEBoundary4A7AE55984E7438034
  content-type: application/xop+xml; charset=utf-8; type="application/soap+xml;"
  content-transfer-encoding: binary
  content-id: &lt;0.09BC7F4BE2E4D3EF1B@apache.org&gt;
  
  &lt;?xml version='1.0' encoding='utf-8'?&gt;
  &lt;soapenv:Envelope xmlns:soapenv="...."....&gt;
    ........
           &lt;xop:Include href="cid:1.A91D6D2E3D7AC4D580@apache.org" 
                          xmlns:xop="http://www.w3.org/2004/08/xop/include"&gt;
           &lt;/xop:Include&gt;
    ........
  
  &lt;/soapenv:Envelope&gt;
  --MIMEBoundary4A7AE55984E7438034
  content-type: application/octet-stream
  content-transfer-encoding: binary
  content-id: &lt;1.A91D6D2E3D7AC4D580@apache.org&gt;
  
  Binary Data.....
  --MIMEBoundary4A7AE55984E7438034--
  </code></pre>
  
  <p>I have tried to follow the documentation and the sample code in the spring-ws samples and for some reason the output i am getting is always this (i.e. the base64 data is not an attachement.</p>
  
  <pre><code>HTTP/1.1 200 OK
  Server: Apache-Coyote/1.1
  Accept: text/xml, text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
  SOAPAction: ""
  Content-Type: text/xml;charset=utf-8
  Content-Length: 4750
  Date: Tue, 03 Jul 2012 17:05:21 GMT
  
  &lt;SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"&gt;&lt;SOAP-ENV:Header/&gt;&lt;SOAP-ENV:Body&gt;&lt;ns2:downloadMessageResponse xmlns:ns2="http://ws.mypackage.com"&gt;&lt;ns2:payLoad&gt;&lt;ns2:messagePayLoad&gt;....iVBORw0KGgoAAAANSUhEUgAAAFoAAAAeCyAAAAAElFTkSuQmCC....&lt;/ns2:messagePayLoad&gt;&lt;/ns2:payLoad&gt;&lt;/ns2:downloadMessageResponse&gt;&lt;/SOAP-ENV:Body&gt;&lt;/SOAP-ENV:Envelope&gt;
  </code></pre>
  
  <p>As you can see, the payload is not an attachment. Here is how i have configured my application:</p>
  
  <p>web.xml</p>
  
  <pre><code>&lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;
          /WEB-INF/app-config.xml
          &lt;/param-value&gt;
      &lt;/context-param&gt;
  
      &lt;listener&gt;
          &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
      &lt;/listener&gt;
  
      &lt;servlet&gt;
          &lt;servlet-name&gt;webservice&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.springframework.ws.transport.http.MessageDispatcherServlet&lt;/servlet-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
              &lt;param-value&gt;/WEB-INF/ws-config.xml&lt;/param-value&gt;
          &lt;/init-param&gt;
      &lt;/servlet&gt;
  </code></pre>
  
  <p>ws-config.xml</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:ws="http://www.springframework.org/schema/web-services"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/web-services 
      http://www.springframework.org/schema/web-services/web-services-2.0.xsd
          http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/context 
          http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;
  
      &lt;context:component-scan base-package="com.mypackage"/&gt;
  
      &lt;ws:annotation-driven/&gt;
  
  
      &lt;ws:dynamic-wsdl id="serviceDefinition" portTypeName="myService"
                       locationUri="http://localhost:8080/springWsTest/webservice"&gt;
          &lt;ws:xsd location="/WEB-INF/schemas/downloadMessageRequest.xsd"/&gt;
      &lt;/ws:dynamic-wsdl&gt;
  
      &lt;bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller"&gt;
          &lt;property name="contextPath" value="com.mypackage.ws"/&gt;
          &lt;property name="mtomEnabled" value="true"/&gt;
      &lt;/bean&gt; 
  &lt;/beans&gt;
  </code></pre>
  
  <p>downloadMessageRequest.xsd schema file</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
  xmlns:m="http://ws.mypackage.com" 
  xmlns:xmime="http://www.w3.org/2005/05/xmlmime" elementFormDefault="qualified"
  targetNamespace="http://ws.mypackage.com" 
  attributeFormDefault="unqualified"&gt; 
  
      &lt;xs:element name="downloadMessageRequest"&gt;
          &lt;xs:complexType/&gt;
      &lt;/xs:element&gt;
  
      &lt;xs:element name="downloadMessageResponse" type="m:downloadResponseType" /&gt;
  
      &lt;xs:complexType name="downloadResponseType"&gt;
              &lt;xs:sequence&gt;
                  &lt;xs:element name="requestName" type="xs:string"/&gt;
                  &lt;xs:element name="payLoad"&gt;
                      &lt;xs:complexType&gt;
                          &lt;xs:sequence&gt;
                              &lt;xs:element name="messagePayLoad" type="xs:base64Binary" xmime:expectedContentTypes="application/octet-stream"/&gt;
                          &lt;/xs:sequence&gt;
                      &lt;/xs:complexType&gt;
                  &lt;/xs:element&gt;
              &lt;/xs:sequence&gt;
       &lt;/xs:complexType&gt;
  
  
       &lt;xs:element name="localDTMRequest"&gt;
          &lt;xs:complexType/&gt;
      &lt;/xs:element&gt;
  
      &lt;xs:element name="localDTMResponse"&gt;
          &lt;xs:complexType&gt;        
              &lt;xs:sequence&gt;
                  &lt;xs:element name="localDTM" type="xs:dateTime"/&gt;
              &lt;/xs:sequence&gt;          
          &lt;/xs:complexType&gt;
      &lt;/xs:element&gt;
  
  
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>The file does get converted to base64binary. The JAXB clases are generated correctly. The Endpoint works but it is not including the file as an attachement. It is including it as part of the XML tag even though i have set mtomEnabled=true.  </p>
  
  <p>What am i missing? </p>