Igor Kromin |   Consultant. Coder. Blogger. Tinkerer. Gamer.

NOTE: This article is 3 years or older so its information may no longer be relevant. Read on at your own discretion! Comments for this article have automatically been locked, refer to the FAQ for more details.
This is an example showing how to create an MTOM/XOP enabled web service that can be deployed in WebLogic.

For those who don't know what it is (from Wikipedia):
MTOM is the W3C Message Transmission Optimization Mechanism, a method of efficiently sending binary data to and from Web services.

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.
package net.igorkromin;
import javax.activation.DataHandler;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlMimeType;
import javax.xml.ws.BindingType;
import javax.xml.ws.Holder;
import javax.xml.ws.soap.MTOM;
@BindingType(value = javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING)
@MTOM (enabled = true, threshold = 0)
public class WsExampleImpl {
public WsExampleImpl() {
public void getData(
@WebParam(mode = WebParam.Mode.IN, name = "dataId")
String dataId,
@WebParam(mode = WebParam.Mode.OUT, name = "data")
Holder<DataHandler> data
) {
String returnData = "Some data with ID:" + dataId;
DataHandler dh =
new DataHandler(returnData, "text/plain");
data.value = dh;

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:
 SOAP request
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:igor="http://igorkromin.net">

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.
 SOAP response (inline Base64)
<S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envelope">
<ns0:getDataResponse xmlns:ns0="http://igorkromin.net">

However, once the correct MTOM headers are set on the request message, the response changes accordingly:
 SOAP response (MTOM/XOP)
<S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envelope">
<ns0:getDataResponse xmlns:ns0="http://igorkromin.net">
<xop:Include href="cid:[email protected]" xmlns:xop="http://www.w3.org/2004/08/xop/include"/>

...in addition there is an attachment that is referenced from inside the xop:Include element:
 SOAP response attachment
Content-Id: <[email protected]>
Content-Type: text/plain
Content-Transfer-Encoding: binary
Some data with ID:42

So that's all there is to it. Enjoy.


Hope you found this post useful...

...so please read on! I love writing articles that provide beneficial information, tips and examples to my readers. All information on my blog is provided free of charge and I encourage you to share it as you wish. There is a small favour I ask in return however - engage in comments below, provide feedback, and if you see mistakes let me know.

If you want to show additional support and help me pay for web hosting and domain name registration, donations, no matter how small, are always welcome!

Use of any information contained in this blog post/article is subject to this disclaimer.
comments powered by Disqus
Other posts you may like...