Spring adding two elements to the SOAP header

a 夏天 提交于 2019-12-06 11:38:39

Managed to solve the problem, Here's how :

Instead of marshalling the two elements into StringResult objects and then trying to add them to the header using a the Transformer like this :

StringResult stringResult = new StringResult();
webServiceTemplate.getMarshaller().marshal(element, stringResult);
StringSource headerSource = new StringSource(stringResult.toString());
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(headerSource, soapHeader.getResult());

You can marshal the two elements directly into the soap header like this :

webServiceTemplate.getMarshaller().marshal(element1,soapHeader.getResult());
webServiceTemplate.getMarshaller().marshal(element2,soapHeader.getResult());

The marshaller mentioned here is a "org.springframework.oxm.jaxb.Jaxb2Marshaller" The element 1 and 2 above are the JAXB elements created using the generated Object factory class.

With this approach, there is no need to add dummy root elements.

Hope this helps someone out there, and thanks to Grigori.G for pointing me in the right direction !

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!