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

你离开我真会死。 提交于 2019-12-18 09:26:37

问题


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

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