如何在java中的soap头中添加子元素

时间:2013-05-01 16:52:32

标签: java xml web-services spring

如何在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());

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

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

3 个答案:

答案 0 :(得分:9)

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

“终于”,我昨晚能够通过一种解决方法,从Spring org.springframework.ws.soap.SoapHeader转换为javax.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 header子元素中!

答案 1 :(得分:3)

JAX-WS用户

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

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

Spring Web Services用户

存在类似的概念:

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

问题表明它无法理解tem命名空间,因此可能在标题中注册命名空间:

soapHeader.addNamespaceDeclaration("tem", "http://whatever.namespace.com/");

答案 2 :(得分:0)

使用&#34; header = true&#34; webparam中的财产 喜欢

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

按照上面的例子&#34; yourBeanObj&#34;将被添加到肥皂体中,&#34;用户名&#34;和#34;密码&#34;添加肥皂标题。