Java – how do I add soap headers to the spring Jax WS client?

How do I add soap headers to the spring Jax WS client?

Specifically, I have a JAXB object that I want to add to the title, but the XML example will be appreciated

I'm using spring's jaxwsportproxyfactory bean to describe here In addition, I am generating my client, as described here, which reduces the titles I need to add

thank you.

Solution

A little elegance (one more class):

public void doWithMessage(WebServiceMessage message) {
    try {
        SOAPMessage soapMessage = ((saajSoapMessage)message).getsaajMessage();
        SOAPHeader header = soapMessage.getSOAPHeader();
        SOAPHeaderElement security = header.addHeaderElement(new QName("http://schemas.xmlsoap.org/ws/2003/06/secext","Security","wsse"));
        SOAPElement usernameToken = security.addChildElement("UsernameToken","wsse");
        SOAPElement username = usernameToken.addChildElement("Username","wsse");
        SOAPElement password = usernameToken.addChildElement("Password","wsse");

        username.setTextContent(someUsername);
        password.setTextContent(somePassword);
    } catch (Exception e) {
       //... handle appropriately
    }
}

Note: this example already uses spring WS 2.1 4 was tested

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>