使用CXF

时间:2017-08-27 19:43:21

标签: java web-services cxf

如何在Web Service客户端请求上添加一个类似于XML的头? 下面的xml是SOAP UI自动生成的请求,并且在给定正确的用户名/密码的情况下工作正常。

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:dof="http://dof.ad.com">
   <soapenv:Header>
      <dof:UserCredentials>
         <!--Optional:-->
         <dof:userName></dof:userName>
         <!--Optional:-->
         <dof:password></dof:password>
      </dof:UserCredentials>
   </soapenv:Header>
   <soapenv:Body>
      <dof:CheckService/>
   </soapenv:Body>
</soapenv:Envelope>

我想知道如何使用apache CXF添加标题部分。我使用来自WSDL的CXF自动生成代码(见下文),并检查了各种文章,但代码仍无法正常工作。我假设发送的请求不正确或者没有标题。

public static void checkServiceMan() {
    String address = "https://WSDL_URL";

    JaxWsProxyFactoryBean jaxWsProxy = new JaxWsProxyFactoryBean();
    jaxWsProxy.setServiceClass(DOFairservice.class);
    jaxWsProxy.setAddress(address);

    DOFairservice serviceClient = (DOFairservice) jaxWsProxy.create();

    ObjectFactory factory = new ObjectFactory();
    UserCredentials uc = factory.createUserCredentials();
    uc.setUserName("username");
    uc.setPassword("password");

    List<Header> headerList = new ArrayList<Header>();
    try {

        Header testCredentialsHeader = new Header(new QName("http://DOF", "DOFairservice")
                ,uc
                ,new JAXBDataBinding(UserCredentials.class));

        headerList.add(testCredentialsHeader);

    } catch (JAXBException e) {

        e.printStackTrace();
    }
    ((BindingProvider) serviceClient).getResponseContext().put(Header.HEADER_LIST, headerList);

    Client client = ClientProxy.getClient(serviceClient);
    HTTPConduit conduit = (HTTPConduit) client.getConduit();
    HTTPClientPolicy policy = conduit.getClient();
    policy.setReceiveTimeout(10000);

    String resp = serviceClient.checkService(); **<--- ERROR HERE**
    System.out.println("Response from Customs Web Service: "+ resp);

    System.out.println("Web Service Header Test Done");
}

错误消息

Exception in thread "main" javax.xml.ws.soap.SOAPFaultException: Error reading XMLStreamReader: Unexpected character '8' (code 56) in prolog; expected '<'
 at [row,col {unknown-source}]: [1,1]
    at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:161)
    at com.sun.proxy.$Proxy45.checkService(Unknown Source)
    at ae.abudhabi.dof.client.CheckServiceClient.checkServiceMan(CheckServiceClient.java:103)
    at ae.abudhabi.dof.client.CheckServiceClient.main(CheckServiceClient.java:112)
Caused by: com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '8' (code 56) in prolog; expected '<'

1 个答案:

答案 0 :(得分:1)

我找到了一个简单的解决方案,可以在我发送给Web服务的请求上添加标头。 我需要做的只是在我的Maven CXF插件构建中添加下面的额外代码。

<extendedSoapHeaders>true</extendedSoapHeaders>

上面的额外行仅在wsdl中显式声明soap标题部分时才有效。

<wsdl:input>
<soap:body use="literal"/>
<soap:header message="tns:CheckUserCredentials" part="UserCredentials" use="literal"/>
</wsdl:input>

我重新生成了源代码并进行了修改,见下文

public static void checkServiceMan() {
    String address = "https://WSDL_URL";

    JaxWsProxyFactoryBean jaxWsProxy = new JaxWsProxyFactoryBean();
    jaxWsProxy.setServiceClass(DOFairservice.class);
    jaxWsProxy.setAddress(address);

    DOFairservice serviceClient = (DOFairservice) jaxWsProxy.create();

    ObjectFactory factory = new ObjectFactory();
    UserCredentials uc = factory.createUserCredentials();
    uc.setUserName("username");
    uc.setPassword("password");

    String resp = serviceClient.checkService(uc); **<--- New Argument Here**
    System.out.println("Response from Customs Web Service: "+ resp);

    System.out.println("Web Service Header Test Done");
}

一切正常,标题现已插入发送到Web服务的XML请求中。