Apache CXF添加自定义标头

时间:2015-11-12 19:38:33

标签: scala soap wsdl cxf

我必须生成以下SOAP内容:

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

1 个答案:

答案 0 :(得分:1)

我想出了怎么做:

    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]))
    )
相关问题