Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add child element in soap header in java

How to add child element in soap header in java spring webservice.

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.

like image 371
user1047873 Avatar asked May 01 '13 16:05

user1047873


People also ask

How do you add elements to a SOAP header in Java?

By using List<String> object, you can add multiple SOAP header elements that each have the same QName object. Set the HashMap object as a property on the request context of the Dispatch or Proxy object.

How do you add mustUnderstand to SOAP header?

The SOAP mustUnderstand attribute can be used to indicate whether a header entry is mandatory or optional for the recipient to process. If you add mustUnderstand="1" to a child element of the Header element it indicates that the receiver processing the Header must recognize the element.

What goes in a SOAP header?

The SOAP <Header> is an optional element in a SOAP message. It is used to pass application-related information that is to be processed by SOAP nodes along the message path. The immediate child elements of the <Header> element are called header blocks.


1 Answers

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!

"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!

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!

like image 82
Rohit Bansal Avatar answered Sep 27 '22 00:09

Rohit Bansal