SOAP MTOM / XOP template using Java 8, Spring, JAXB
I had a lot of trouble figuring out the best way to do this. So I put this here for others that are looking for a quick how-to accept and send binary files through an HTTP end point, and more specifically, to send MTOM / XOP attachments in a SOAP request.
This isn't a completely running example obviously. It is a skeleton of everything you need if you want to send or receive MTOM / XOP SOAP requests.
Pretty straight forward service oriented architecture. Controller accepts a request. First, Spring unmarshalles the file into a "MultiPartFile". Controller then converts request into DTO then passes it so Service. This is where business logic goes. Then it is passed to a WebService layer where the outgoing request is made.
MTOM or the Message Transmission Optimization Mechanism is a low overhead way of sending attachments in a SOAP message
There are only two things that you have to do in addition to normal SOAP messages:
- Enable MTOM. There are several ways to do this. In this example, inside the MtomClient.java class, you can see it is enabled through the JaxBMarshaller
- Add a SoapAction. One would expect this to happen automatically with the marshaller. That is not the case. Instead, you have to manually set the SOAPAction. Inside the MyWebService.java send() method, you can see I've added a WebServiceMessageCallback that adds it. This method gets called once the SOAP request is ready to be accepted and will tell the server where to send the message. The SOAPACtion is explicitly defined in the WSDL and it can be taken from there and hard-coded.
We create an object of type FormData and add a file to it. There are some good resources for creating HTML forms that accept a file online.. We just have to call the method in import-file.js when the user hits "submit" and pass the file that is selected.