I'm building a Web Services server that's designed to share content. I'd like to serve the content through a SOAP response containing an attachment. Right now, I'm using Spring WS to handle requests and serve responses.
My service resembles something like this:
@Endpoint
public class Service{
@PayloadRoot(namespace = "http://foo.com/coffee", localPart = "order")
@ResponsePayload
public Coffee getCoffee(@RequestPayload Order order){
return new Coffee("Hot Joe");
}
}
But suppose I want to attach a picture of a cup of coffee to the response, where and how do I do that?
edit: as an aside, the examples that ship with Spring-WS show how to use the client to send an attachment, but not how the server should respond with one (which is what I'm asking about here).
The documentation in Spring-WS is particularly light on this topic, and it's actually pretty easy to add a SOAP attachment. I'll make a few assumptions:
mime:multipartRelated
on the output messageAttachments reside in the MimeContainer on the SOAP message. To get this container, we need to manually construct the SOAP response, well, just part of it. Doing that looks like this:
Now we need a DataHandler for our picture:
Okay, content: check, message: check, hm, still need to get the response to go out. The tricky part here is that we need to bring in the
MessageContext
so that we can set this particular message to be the one we respond with, we do that by editing our definition ofgetCoffee
:Putting it all together:
For good measure, here's the beans dependency injection for getting a
SaajMessageFactory
: