How can I add SOAP Headers to Spring Jax-WS Client?
Specifically, I have a Jaxb object I would like to add to the header but xml examples would be appreciated.
I'm still trying to find an elegant way to add headers, but what I do as suggested by others is to use a Transformer on the WebServiceMessageCallBack(). Here's an example code:
JAXBElement result = (JAXBElement) webServiceTemplate.marshalSendAndReceive(request, new WebServiceMessageCallback() {
public void doWithMessage(WebServiceMessage webServiceMessage) {
try {
SoapMessage soapMessage = (SoapMessage) webServiceMessage;
soapMessage.setSoapAction("getDeleted");
SoapHeader header = soapMessage.getSoapHeader();
StringSource headerSource = new StringSource("\n" +
""+"johnsmith"+" \n" +
""+"1234"+" \n" +
" ");
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(headerSource, header.getResult());
} catch (Exception e) {
new RuntimeException(e);
}
}
...
It's not really elegant, considering this is Spring's WS. It's not intuitive.