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

后端 未结 4 1430
南方客
南方客 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:37

    I'm still trying to find an elegant way to add headers, but what I do as suggested by others is to use a Transformer on the WebServiceMessageCallBack(). Here's an example code:

    JAXBElement result = (JAXBElement) webServiceTemplate.marshalSendAndReceive(request, new WebServiceMessageCallback() {
    public void doWithMessage(WebServiceMessage webServiceMessage) {
        try {
            SoapMessage soapMessage = (SoapMessage) webServiceMessage;
            soapMessage.setSoapAction("getDeleted");
    
            SoapHeader header = soapMessage.getSoapHeader();
            StringSource headerSource = new StringSource("\n" +
                                    ""+"johnsmith"+"\n" +
                                    ""+"1234"+"\n" +
                                    "");
            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.transform(headerSource, header.getResult());
    
           } catch (Exception e) {
             new RuntimeException(e);
           }
    }
    ...
    

    It's not really elegant, considering this is Spring's WS. It's not intuitive.

提交回复
热议问题