1) Annotating the Message
1a) Modifying your schema for MTOM
Lets say we have a Picture schema type like this:
<schema targetNamespace="http://pictures.com"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<element name="Picture">
<complexType>
<sequence>
<element name="Title" type="xsd:string"/>
<element name="ImageData" type="xsd:base64Binary"/>
</sequence>
</complexType>
</element>
</schema>
In this case the ImageData element is something we would like to have transferred as an attachment. To do this we just need to add an xmime:expectedContentTypes annotation:
<schema targetNamespace="http://pictures.com"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xmime="http://www.w3.org/2005/05/xmlmime">
<element name="Picture">
<complexType>
<sequence>
<element name="Title" type="xsd:string"/>
<element name="ImageData" type="xsd:base64Binary"
xmime:expectedContentTypes="application/octet-stream"/>
</sequence>
</complexType>
</element>
</schema>
This tells JAXB (which WSDL2Java uses to generate POJOs for your service) that this field could be of any content type. Instead of creating a byte[] array for the base64Binary element, it will create a DataHandler instead which can be used to stream the data.
1b) Annotation your JAXB beans to enable MTOM
If you're doing code first, you need to add an annotation to your POJO to tell JAXB that the field is a candidate for MTOM optimization. Lets say we have a Picture class with has Title and ImageData fields, then it might look like this:
@XmlType
public class Picture {
private String title;
@XmlMimeType("application/octet-stream")
private DataHandler imageData;
public String getTitle() { return title; }
public void setTitle(String title) { this.title = title; }
public DataHandler getImageData() { return imageData; }
public void setImageData(DataHandler imageData)
{ this.imageData = imageData; }
}
Note the use of 'application/octet-stream'. According to the standard, you should be able to use any MIME type you like, in order to specify the actual content of the attachment. However, due to a defect in the JAX-B reference implementation, this won't work.