Monday, October 5, 2009

Java Code Sinppet: JAX-WS & MTOM

To enable MTOM (Message Transmission Optimization Mechanism) there are several things need to be done and in a nutshell

1. add the MTOM annotation on the services class
@WebService(serviceName =

2. Following table list the relationship between MIME type and java type

MIME type Java type
image/gif java.awt.Image
image/jpeg java.awt.Image
text/plain java.lang.String
text/xml javax.xml.transform.Source
application/xml javax.xml.transform.Source
*/* javax.activation.DataHandler

3. Edit the WSDL to and list the correct mime type which will change the schema from

<xs:element name="return" type="xs:base64Binary" minOccurs="0" maxOccurs="unbounded"/>


<xs:element name="return" type="xs:base64Binary" minOccurs="0" maxOccurs="unbounded"
xmime:expectedContentTypes="image/jpeg" xmlns:xmime=""/>

4. Make the container use the edited WSDL than generated one

@WebService(serviceName = ...., wsdlLocation="WEB-INF/wsdl/edited.wsdl")

5. Deploy the new service

for more info netbeans tutorial pages 1, page 2, page 3

To do the above without any modification to wsdl (using annotation on the java class)

1. Set the @MTOM as above on the service
public class NewWebService {

2. Annotate the data type on the web method as follows
public void setImage(@XmlMimeType("image/gif")Image img){

3. Can also annotate a return type
public @XmlMimeType("image/gif")Image getImage( String name){

4. All kind of data types could be used
public @XmlMimeType("*/*")Image getFiles