java 如何将 wsdl 中定义的 Soap Header 添加到 CXF 中的 Web 服务客户端?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11767209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:17:39  来源:igfitidea点击:

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

javasoapcxf

提问by ScArcher2

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

我有一个 wsdl,它定义了一个在调用 Web 服务时需要传递的soap 标头。

The sample SOAP Header is:

示例 SOAP 标头是:

<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.

CXF 的 wsdl2java 生成了一个“AuthenticationInfo”java 类,我可以创建并填充用户名和密码,但我不知道在调用 Web 服务时将其传递给 CXF 客户端的正确方法。

回答by soumitra chatterjee

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.

使用 Apache CXF 生成代理类时,使用添加扩展的SoapHeaders 将生成带有请求和标头参数的 PortType 类。

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

回答by afrin216

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

好吧,最简单的方法是创建一个ArrayListofHeader对象并添加所有参数或 aMap<String,Object>并将所有标题添加为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.

如果您尝试传递自定义soap 标头,请参阅此链接

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

本次讨论中已经提到了一般的陷阱。它可能对你有帮助。

回答by CodeNotFound

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

如果 SOAP 标头是在 WSDL 中定义的,那么它可以被指定为隐式或显式

CXF provides the wsdl2java toolfor 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.

CXF 提供了wsdl2java 工具,用于从 WSDL 生成 Java 服务接口。在显式标头的情况下,SOAP 标头会被自动检测并作为生成的服务接口的一部分提供。

If the SOAP headers have been defined implicitly, then you need to enable the -exshoption 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.

如果隐式定义了 SOAP 标头,则需要启用-exsh触发隐式 SOAP 标头处理的 选项。同样,SOAP 标头将作为生成的服务 Java 接口的一部分提供。如果你想要一个具体的例子,你可以查看我写的一篇关于如何添加 cxf 肥皂头的博客文章。

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

请注意,CXF 还支持添加 SOAP 标头的其他方式

回答by Alex

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

发现自己处于相同的情况:wsdl2java生成了标头类,我需要将它作为 SOAP 标头添加到传出的 SOAP 请求中。

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

我在代码中的解决方案如下(重用原始问题AuthenticationInfo作为标题类名称):

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);