2015-11-12 80 views
0

我不得不产生以下SOAP内容:的Apache CXF添加自定义页眉

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://www.myService.com/MyServices/"> 
    <soapenv:Header> 
     <ns:UserCredentials> 
     <ns:UserName>?</ns:UserName> 
     <ns:Password>?</ns:Password> 
     </ns:UserCredentials> 
    </soapenv:Header> 
    <soapenv:Body> 
     <ns:MyOperation> 
     <ns:Settings> 
      <!--1 or more repetitions:--> 
      <ns:Setting> 
       <ns:Name>?</ns:Name> 
       <ns:Value>?</ns:Value> 
      </ns:Setting> 
     </ns:Settings> 
     </ns:MyOperation> 
    </soapenv:Body> 
</soapenv:Envelope> 

我做我的代码如下:

val factory = new org.apache.cxf.jaxws.JaxWsProxyFactoryBean() 
    factory.setServiceClass(classOf[MyServicesSoap]) 
    factory.setAddress("http://127.0.0.1/my-service/WebService") 
    factory.getInInterceptors.add(new org.apache.cxf.interceptor.LoggingInInterceptor()) 
    factory.getOutInterceptors.add(new org.apache.cxf.interceptor.LoggingOutInterceptor()) 
    val myServiceClient = factory.create().asInstanceOf[MyServicesSoap] 
    val proxy = ClientProxy.getClient(myServiceClient) 

    // TODO: get all this shit below from config! 
    val headerList = Seq(
     new Header(new QName("http://www.myService.com/MyServices/", "UserName"), "", new JAXBDataBinding(classOf[String])), 
     new Header(new QName("http://www.myService.com/MyServices//", "Password"), "", new JAXBDataBinding(classOf[String])) 
    ) 

    import scala.collection.JavaConverters._ 
    proxy.getRequestContext.put(Header.HEADER_LIST, headerList.asJava) 

    val result = myServiceClient.getMyList 

    println(result) 

我最终得到一个SOAP如下(只头所显示的因为这是有点问题):

<soap:Envelope 
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soap:Header> 
     <UserName xmlns="http://www.myservice.com/MyServices/"> 
     </UserName> 
     <Password xmlns="http://www.myservice.com/MyServices/"> 
     </Password> 
    </soap:Header> 
     <soap:Body> 

     </soap:Body> 
    </soap:Envelope> 

回答

0

我想出如何做到这一点:

val userCredentials = new UserCredentials 
    userCredentials.setPassword("") 
    userCredentials.setUserName("") 

    // TODO: get all this shit below from config! 
    val headerList = Seq(
     new Header(new QName("https://www.myservice.com/MyService/", "UserCredentials"), userCredentials, new JAXBDataBinding(classOf[UserCredentials])) 
    )