How to add SOAP Headers to Spring Jax-WS Client?

后端 未结 4 1438
南方客
南方客 2021-02-02 16:10

How can I add SOAP Headers to Spring Jax-WS Client?

Specifically, I have a Jaxb object I would like to add to the header but xml examples would be appreciated.

4条回答
  •  无人共我
    2021-02-02 16:50

    A little bit more elegant (still a class cast is required):

    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 has been testes with Spring WS 2.1.4.

提交回复
热议问题