简体   繁体   中英

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.

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!

JAX-WS Users

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

Spring Web Services Users

A similar concept exists:

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:

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

Use "header=true" property in webparam 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
}

As per above example "yourBeanObj" wil be added into soap body, "username" and "password" are add in soap header.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM