How do you add a Soap Header defined in a wsdl to a web service client in CXF?

24,730

Solution 1

Well, the most simple way to do this would be create an ArrayList of Header objects and add all your parameters or a Map<String,Object> and add all your headers as map.put("param1",param1).

Finally get your request context and add this arraylist of map as

requestContext.put(MessageContext.HTTP_REQUEST_HEADERS,
soapHeaders); 

If you're trying to pass custom soap headers, refer THIS LINK.

The general pitfalls have been mentioned in THIS DISCUSSION. It might be helpful to you.

Solution 2

While generating the proxy class using Apache CXF using adding the extendedSoapHeaders with true will generate the PortType Class with the Request and Header argument.

<wsdlOption>              
<wsdl>${project.basedir}/src/main/resources/wsdl/sample.wsdl</wsdl>
 <!-- enables processing of implicit SOAP headers, default is false -->
<extendedSoapHeaders>true</extendedSoapHeaders>
</wsdlOption>

Solution 3

If the SOAP header is defined in the WSDL then it can either be specified implicit or explicit.

CXF provides the wsdl2java tool for generating a Java service interface from a WSDL. In the case of explicit headers, the SOAP headers are automatically detected and made available as part of the service interface that gets generated.

If the SOAP headers have been defined implicitly, then you need to enable the -exsh option which triggers processing of implicit SOAP headers. Again the SOAP headers will be made available as part of the service Java interface that gets generated. If you want a concrete example you can checkout a blog post I made on how to add a cxf soap header.

Note that CXF also supports other ways of adding SOAP headers.

Solution 4

Found myself in same situation: wsdl2java generated the header class, and I needed to add it as a SOAP header to the outgoing SOAP request.

My solution in code was as follows (reusing original question's AuthenticationInfo as the header class name):

import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.headers.Header;

AuthenticationInfo ai = new AuthenticationInfo();
ai.setUserName("User");
ai.setPassword("");

List<Header> soapHeaders = new ArrayList<Header>();

Header h1 = new Header(new QName("http://namespace/of/AuthenticationInfo", "AuthenticationInfo"), 
                       ai, new JAXBDataBinding(AuthenticationInfo.class));

soapHeaders.add(h1);

ClientProxy.getClient(port).getRequestContext().put(Header.HEADER_LIST, soapHeaders);
Share:
24,730
ScArcher2
Author by

ScArcher2

I'm a developer that grew up on Java, worked in C# and Ruby. I'm interested in Functional Programming and am attempting to learn Clojure.

Updated on October 05, 2021

Comments

  • ScArcher2
    ScArcher2 over 2 years

    I have a wsdl that defines a soap header that needs to be passed when calling the web service.

    The sample SOAP Header is:

    <soapenv:Header>
       <AuthenticationInfo>
          <userName>User</userName>
          <password/>
       </AuthenticationInfo>
    </soapenv:Header>
    

    CXF's wsdl2java generated an "AuthenticationInfo" java class that I can create and populate with a username and password, but I don't know the proper way to pass that to the CXF Client when calling the web service.