java 如何在java中的soap header中添加子元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16322634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add child element in soap header in java
提问by user1047873
How to add child element in soap header in java spring webservice.
如何在java spring webservice的soap header中添加子元素。
I have tried two approach but nothing is working please suggest me what need to be done?
我尝试了两种方法但没有任何效果请建议我需要做什么?
first approach :-
第一种方法:-
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());
but it is giving me error
但它给了我错误
The prefix "tem" for element "tem:Culture" is not bound.
2nd Approach :-
第二种方法:-
SoapHeaderElement headerElement=soapMessage.getSoapHeader().addHeaderElement(new QName("http://tempuri.org/", "SOAPHeaderAuth","tem"));
headerElement.setText(soapHeaderString.toString());
It produce unescaped sequqnce so service provider give error as it was not able to understand the request.
它产生未转义的序列,因此服务提供者由于无法理解请求而给出错误。
Please help me what need to be done to solve the problem.
请帮助我需要做什么来解决问题。
回答by Rohit Bansal
Yes, in my case too it prompts the same error, The prefix "username" for element "wsse:username" is not bound. I tried doing everything to add namespace declaration. But, it doesn't worked!
是的,在我的情况下,它也提示相同的错误,元素“wsse:username”的前缀“username”未绑定。我尝试尽一切努力添加命名空间声明。但是,它不起作用!
"Finally", I was able to do it last night with a workaround, converting some way from Spring org.springframework.ws.soap.SoapHeader
to javax.xml.soap.SOAPHeader
. And no issues now!
“终于”,我昨晚能够通过一种变通方法做到这一点,将某种方式从 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);
Through this I was able to add namespace declarations to spring soap header child elements!
通过这个,我能够将命名空间声明添加到 spring 肥皂头子元素!
回答by davidfmatheson
JAX-WS Users
JAX-WS 用户
I would recommend using a message handler:
我建议使用消息处理程序:
http://www.javadb.com/using-a-message-handler-to-alter-the-soap-header-in-a-web-service-client
http://www.javadb.com/using-a-message-handler-to-alter-the-soap-header-in-a-web-service-client
Spring Web Services Users
Spring Web 服务用户
A similar concept exists:
存在类似的概念:
http://arcanetechnotes.blogspot.com/2008/12/modifying-soap-headers-with-spring-web.html
http://arcanetechnotes.blogspot.com/2008/12/modifying-soap-headers-with-spring-web.html
The question states that it cannot understand the tem
namespace, so perhaps register the namespace in the header:
问题指出它无法理解tem
命名空间,因此可能在标头中注册命名空间:
soapHeader.addNamespaceDeclaration("tem", "http://whatever.namespace.com/");
回答by kishore
Use "header=true" property in webparam Like
在 webparam 中使用“header=true”属性
@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
}
As per above example "yourBeanObj" wil be added into soap body, "username" and "password" are add in soap header.
按照上面的例子,“yourBeanObj”将被添加到soap主体中,“用户名”和“密码”被添加到soap头中。