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

后端 未结 4 1788
自闭症患者
自闭症患者 2020-12-11 10:55

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

The sample SOAP Header is:


   &         


        
相关标签:
4条回答
  • 2020-12-11 11:36

    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);
    
    0 讨论(0)
  • 2020-12-11 11:42

    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>
    
    0 讨论(0)
  • 2020-12-11 11:44

    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.

    0 讨论(0)
  • 2020-12-11 11:46

    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.

    0 讨论(0)
提交回复
热议问题