How to use MTOM with Axis2 JAX-WS services

If you are new to JAX-WS service development with Axis2, please read this first. In this post, you will find out how to use MTOM functionality in Axis2 JAX-WS services. Here I’m using Axis2 1.5.1.

First of all, you have to use the BindingType annotation to set the SOAP11 MTOM binding as follows.

@BindingType(SOAPBinding.SOAP11HTTP_MTOM_BINDING)

After that, you can use javax.activation.DataHandler to represent your binary data in your parameters or return types. A simple MTOM enabled JAX-WS service class can be written as follows.

@WebService(serviceName = “MTOMSampleService”,
targetNamespace = “http://mtom.jaxws.wso2.org”
)
@BindingType(SOAPBinding.SOAP11HTTP_MTOM_BINDING)

public class MTOMService {

@WebMethod(action = “urn:uploadFile”)
public String uploadFile(DataHandler data) {
try {
InputStream is = data.getInputStream();
….
String msg = “File ” + data.getName() + ” of type ” + data.getContentType() +
” successfully received”;
return msg;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

@WebMethod(action = “urn:getTestData”)
public DataHandler getBinaryTestData(String stmtId) {
byte[] testData = new byte[10240];

for (int i = 0; i < testData.length; i++) {
testData[i] = 0x7f;
}

ByteArrayDataSource bds = new ByteArrayDataSource(
testData, “application/octet-stream”);
return new DataHandler(bds);

}

}

DataHandler objects can be used inside wrapped classes as well.

Advertisements

About isurues
Age : 24 Date of Birth : 05.11.1984 Country : Sri Lanka

One Response to How to use MTOM with Axis2 JAX-WS services

  1. Dale says:

    Hi Isuru.

    I have problem with download very large file (near 2GB) (small files up to 5 MB – ok).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: