如何向
Spring Jax-WS客户端添加SOAP头?
具体来说,我有一个Jaxb对象,我想添加到标题,但xml示例将不胜感激.
我正在使用Spring的JaxWsPortProxyfactorybean描述here.另外,我正在生成我的客户端,如所描述的here,它减少了我需要添加的标题.
谢谢.
解决方法
一点点优雅(还需要一个班级):
public void doWithMessage(WebServiceMessage message) { try { SOAPMessage soapMessage = ((SaajSoapMessage)message).getSaajMessage(); SOAPHeader header = soapMessage.getSOAPHeader(); SOAPHeaderElement security = header.addHeaderElement(new QName("http://schemas.xmlsoap.org/ws/2003/06/secext","Security","wsse")); SOAPElement usernameToken = security.addChildElement("UsernameToken","wsse"); SOAPElement username = usernameToken.addChildElement("Username","wsse"); SOAPElement password = usernameToken.addChildElement("Password","wsse"); username.setTextContent(someUsername); password.setTextContent(somePassword); } catch (Exception e) { //... handle appropriately } }
注意:此示例已经使用Spring WS 2.1.4进行了测试.