With CXF (actually GroovyWS), how do I create a SOAP header with one child node with text node?

I am creating a Groovy client for a .net SOAP service that requires a soap header that looks like this:

<soap:Header> <HeaderInfo xmlns="http://foo.bar.com/ns"> <token>abc-unique-token</token> </HeaderInfo> </soap:Header> 

I found faq to add headers to CXF messages and it will deliver me almost there, but not quite. The example they give for option 4 is as follows:

  List<Header> headers = new ArrayList<Header>() Header header = new Header(new QName("http://foo.bar.com/ns", "HeaderInfo"), "abc-unique-token", new JAXBDataBinding(String.class)) headers.add(header) proxy.client.getRequestContext().put(Header.HEADER_LIST, headers) 

Using this code, I can get it to do this:

 <soap:Header> <HeaderInfo xmlns="http://foo.bar.com/ns"> abc-unique-token </HeaderInfo> </soap:Header> 

But in the "HeaderInfo" node there is no child "token" of the node to surround the "abc-unique-token", and I'm not sure how to get it.

Is there some simple thing that I can pass to the header constructor to create the node?

A separate post talks about using a different technique, but it causes errors for me around SoapFactory when I try to use it.

A significant part of the other things I found needs to create something that extends the AbstractPhaseInterceptor class with a bunch of extra code when I want it so close :).

+3
source share
1 answer

I managed to get it to work using this, finding out that the SOAPFactory method in the separate column I was talking about needed saaj-impl.jar to work:

 List<Header> headers = new ArrayList<Header>() SOAPFactory sf = SOAPFactory.newInstance() def authElement = sf.createElement(new QName("http://foo.bar.com/ns", "HeaderInfo")) def tokenElement = authElement.addChildElement("token") tokenElement.addTextNode("abc-unique-token") SoapHeader tokenHeader = new SoapHeader( new QName("http://foo.bar.com/ns", "HeaderInfo"), authElement); headers.add(tokenHeader); proxy.client.getRequestContext().put(Header.HEADER_LIST, headers) 

I'm still curious (and accepting the answer) around this recommended CXF method and adding a node child to the header class.

+5
source

Source: https://habr.com/ru/post/886006/


All Articles