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

故事扮演 提交于 2019-12-23 20:21:25

问题


I'm 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 the faq for adding headers to CXF messages and it gets me almost there, but not quite. The example they give for option 4 looks like this:

    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 the "HeaderInfo" node is missing the child "token" node to surround "abc-unique-token" and I'm not sure how to get it in there.

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

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

Much of the other stuff that I've found gets into needing to create something extending an AbstractPhaseInterceptor class with a bunch of additional code, when what I want is so close :).


回答1:


I was able to get it to work using this after figuring out that the SOAPFactory method in the separate post that I mentioned 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 would accept an answer) around doing it the CXF recommended way and adding a node child to the Header class.



来源:https://stackoverflow.com/questions/3807922/with-cxf-actually-groovyws-how-do-i-generate-a-soap-header-with-one-child-nod

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