5

java spring webservice中如何在soap头中添加子元素。

我尝试了两种方法,但没有任何效果请建议我需要做什么?

第一种方法:-

soapHeaderString.append("<tem:Culture>en_Us</tem:Culture><tem:AgentCode>PumpkinSafari</tem:AgentCode><tem:PartnerID></tem:PartnerID><tem:Password>PMP22#in</tem:Password>");

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
// Here we create a Source Tree
StringSource stringSource = new StringSource(soapHeaderString.toString());
transformer.transform(stringSource, soapHeader.getResult());

但它给了我错误

The prefix "tem" for element "tem:Culture" is not bound.

第二种方法:-

SoapHeaderElement headerElement=soapMessage.getSoapHeader().addHeaderElement(new QName("http://tempuri.org/", "SOAPHeaderAuth","tem"));
headerElement.setText(soapHeaderString.toString());

它会产生未转义的序列,因此服务提供商会给出错误,因为它无法理解请求。

请帮助我解决问题需要做些什么。

4

3 回答 3

13

是的,在我的情况下,它也会提示相同的错误,元素“wsse:username”的前缀“username”未绑定。我尝试尽一切努力添加命名空间声明。但是,它不起作用!

“终于”,昨晚我能够通过一种解决方法做到这一点,将某种方式从 Spring 转换org.springframework.ws.soap.SoapHeaderjavax.xml.soap.SOAPHeader. 现在没有问题了!

SOAPMessage soapMessage = ((SaajSoapMessage) message).getSaajMessage();
SOAPHeader header = soapMessage.getSOAPHeader();
SOAPHeaderElement security = header.addHeaderElement(new QName(SCHEMA, "Security", SCHEMA_PREFIX));
SOAPElement usernameToken = security.addChildElement("UsernameToken", SCHEMA_PREFIX);
SOAPElement usernameElement = usernameToken.addChildElement("Username", SCHEMA_PREFIX);
SOAPElement passwordElement = usernameToken.addChildElement("Password", SCHEMA_PREFIX);

usernameElement.setTextContent(username);
passwordElement.setTextContent(password);

通过这个,我能够将命名空间声明添加到 spring soap 标题子元素!

于 2014-06-13T15:04:17.660 回答
3

JAX-WS 用户

我建议使用消息处理程序:

http://www.javadb.com/using-a-message-handler-to-alter-the-soap-header-in-a-web-service-client

Spring Web 服务用户

存在类似的概念:

http://arcanetechnotes.blogspot.com/2008/12/modifying-soap-headers-with-spring-web.html

问题指出它无法理解tem名称空间,因此可能在标题中注册名称空间:

soapHeader.addNamespaceDeclaration("tem", "http://whatever.namespace.com/");
于 2013-05-01T17:25:43.943 回答
0

在 webparam 中使用“header=true”属性 Like

@WebMethod(operationName = "someMethod")
    public String someMethod(@WebParam(name = "yourBeanObj") YourBeanObjClass yourBeanObj,
            @WebParam(header = true, mode = WebParam.Mode.IN, name = "username") String userName,
            @WebParam(header = true, mode = WebParam.Mode.IN, name = "password") String pwd) {
//todo stuff
}

根据上面的例子,“yourBeanObj”将被添加到肥皂正文中,“用户名”和“密码”被添加到肥皂标题中。

于 2015-10-05T13:50:14.490 回答