Hi! I’m trying to add a header to a SOAP call.
I’m new at working with web services.
Every time I need to use the web service for download a file, I need to add a header like:
<UserIdHeader xmlns="http://www.stats.com/">
<TicketId>defe3a08-4c8a-47c4-9303-98e09c475532</TicketId>
</UserIdHeader>
I’m using CXF. I have one example made with Axis 1.4.
The only thing that is remaining (I hope) is to add this header.
This is what I have to update:
private static void addHeader(String ticketId) {
SOAPHeaderElement header = new SOAPHeaderElement("http://www.stats.com/", "UserIdHeader");
SOAPElement node;
org.apache.axis.client.Stub s = (Stub) service;
s.clearHeaders();
try {
node = header.addChildElement("TicketId");
node.addTextNode(ticketId);
s.setHeader((org.apache.axis.message.SOAPHeaderElement) header);
} catch (SOAPException e) {
e.printStackTrace();
}
}
And this is how I tried to update that:
private static void addHeader(String ticketId) throws JAXBException, ParserConfigurationException {
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
Element eTicketId = doc.createElement("TicketId");
eTicketId.setTextContent(ticketId);
List<Header> headers = new ArrayList<Header>();
Header header = new Header(new QName("http://www.stats.com/", "UserIdHeader"), eTicketId,
new JAXBDataBinding(String.class));
headers.add(header);
BindingProvider.class.cast(service).getRequestContext().put(Header.HEADER_LIST, headers);
}
But I’m getting the following exception:
[PhaseInterceptorChain] Interceptor for {http://www.stats.com/}Service#{http://www.stats.com/}GetFileList has thrown exception, unwinding now
org.apache.cxf.interceptor.Fault: Marshalling Error: org.apache.xerces.dom.ElementImpl is not known to this context
at org.apache.cxf.jaxb.JAXBEncoderDecoder.marshall(JAXBEncoderDecoder.java:261)
at org.apache.cxf.jaxb.io.DataWriterImpl.write(DataWriterImpl.java:168)
at org.apache.cxf.binding.soap.interceptor.SoapOutInterceptor.writeSoapEnvelopeStart(SoapOutInterceptor.java:156)
at org.apache.cxf.binding.soap.interceptor.SoapOutInterceptor.handleMessage(SoapOutInterceptor.java:81)
at org.apache.cxf.binding.soap.interceptor.SoapOutInterceptor.handleMessage(SoapOutInterceptor.java:61)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:262)
at org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:531)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:464)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:367)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:320)
at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:89)
at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:134)
I tried even add an interceptor, but I didn’t know how to pass the ticketId.
Any idea?
If you are creating a DOM element, you shouldn’t set a databinding into the Header object. You only need the Databinding if you are using a JAXB (or other) type. CXF handles DOM elements directly.