Apache CXF 添加自定义 Headers
Apache CXF Adding Custom Headers
我必须生成以下 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(仅显示 header,因为这有点问题):
<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>
我知道怎么做了:
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]))
)
我必须生成以下 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(仅显示 header,因为这有点问题):
<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>
我知道怎么做了:
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]))
)