For those who don't know what it is (from Wikipedia):
MTOM is usually used with the XOP (XML-binary Optimized Packaging).
This example uses SOAP 1.2 bindings. Further, I am specifying the return type as an OUT parameter. I find this a more logical way of returning data from a web method because you can stack up multiple return values this way, instead of being limited to one single return value that Java forces. Hence, the return type of this method is void and the JAX-WS stack handles the rest.
The entire example is built using jwsc and Ant. The build scripts are not shown here.
The service itself is quite trivial, it accepts an input parameter dataId and returns a string with the input parameter concatenated. The DataHandler takes care for most of the work here, the returnData string is simply fed into the data handler. You can also use an output stream here too if you have a large amount of data to return. The mime type of the data is easily changeable too, but for example purposes it is set to text/plain.
So lets look at the SOAP request for this service, without the headers it is quite simple:
Without setting any additional headers to enable MTOM, the response will come back without MTOM/XOP attachments, the data will simply be inline. WebLogic takes care of this automatically, there is nothing further to do in the actual web service to enable this fall-back mechanism.
However, once the correct MTOM headers are set on the request message, the response changes accordingly:
...in addition there is an attachment that is referenced from inside the xop:Include element:
So that's all there is to it. Enjoy.