问题
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.
回答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.
回答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>
回答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.
来源:https://stackoverflow.com/questions/11767209/how-do-you-add-a-soap-header-defined-in-a-wsdl-to-a-web-service-client-in-cxf