使用 cxf-codegen-plugin 的 Soap 标头

问题描述 投票:0回答:1

我正在使用 cxf-codegen-plugin 在 Java 中生成 WSDL 类,我想将 Soap 标头添加到请求中,但它们没有在插件中生成。我如何添加它们?

这是我定义的插件:

<plugin>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-codegen-plugin</artifactId>
            <version>4.0.2</version>
            <executions>
                <execution>
                    <id>generate-sources</id>
                    <phase>generate-sources</phase>
                    <configuration>
                        <defaultOptions>
                            <extraargs>
                                <extraarg>-p</extraarg>
                                <extraarg>com.smartcode.springenterpriseapi.stub.target</extraarg>
                            </extraargs>
                        </defaultOptions>
                        <wsdlOptions>
                            <wsdlOption>
                                <wsdl>${project.basedir}/src/main/resources/enterprise.wsdl</wsdl>
                                <extraargs>
                                    <extraarg>-autoNameResolution</extraarg>
                                </extraargs>
                            </wsdlOption>
                        </wsdlOptions>
                        <includes>
                            <include>${project.build.directory}/generated-sources/cxf/**</include>
                        </includes>
                    </configuration>
                    <goals>
                        <goal>wsdl2java</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

我一直在寻找是否有配置或者如何将其添加到 Java 对象。

java wsdl cxf cxf-codegen-plugin
1个回答
0
投票

我找到了答案。我认为从 cxf-codegen-plugin 生成的类会以某种方式建立肥皂信封与正文和标头的关系,但事实并非如此,您必须添加一个拦截器。

public class HeaderInterceptor extends AbstractPhaseInterceptor <SoapMessage>  {

  public HeaderInterceptor() {
    super(Phase.WRITE);
  }

  @Override
  public void handleMessage(SoapMessage message) throws Fault {
    CustomHeader header = new CustomHeader();
    header.setHeaderValue("Header Value");

    SoapHeader soapHeader = new SoapHeader(new QName("http://example.com/namespace", "CustomHeader"), header, new JAXBDataBinding());
    List < Header > headers = message.getHeaders();
    headers.add(header);
  }
}

然后你建立客户端的时候就可以调用它了。

 Client client = ClientProxy.getClient(soap);
 HeaderInterceptor interceptor = new HeaderInterceptor();
 client.getOutInterceptors().add(interceptor);
 HTTPConduit http = (HTTPConduit) client.getConduit();
 final HTTPClientPolicy policy = http.getClient();
 policy.setAutoRedirect(true);
© www.soinside.com 2019 - 2024. All rights reserved.