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

后端 未结 4 1480
南方客
南方客 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条回答
  •  Happy的楠姐
    2021-02-02 16:42

    After some poking around if found a slightly different solution. I'm using JAXB for marshalling my payload and the possible header classes have also been generated with JAXB from the WSDL. In my case I am addressing Microsoft Reporting Services and have pass on an ExecutionID as SOAP header.

    public class ReportExecution2005Client extends WebServiceGatewaySupport {
    
        private static final String SET_EXECUTION_PARAMETERS_ACTION = "http://schemas.microsoft.com/sqlserver/2005/06/30/reporting/reportingservices/SetExecutionParameters";
    
        private final class SoapActionExecutionIdCallback implements WebServiceMessageCallback {
    
            private final String soapAction;
            private final String executionId;
    
            public SoapActionExecutionIdCallback(String soapAction, String executionId) {
                super();
                this.soapAction = soapAction;
                this.executionId = executionId;
            }
    
            @Override
            public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException {
                SoapMessage soapMessage = (SoapMessage) message;
                soapMessage.setSoapAction(soapAction);
                ExecutionHeader executionHeader = new ExecutionHeader();
                executionHeader.setExecutionID(executionId);
                getMarshaller().marshal(executionHeader, soapMessage.getSoapHeader().getResult());
            }
        }
    
        public void setExecutionParameters(String executionId){
            SetExecutionParameters request = new SetExecutionParameters();
            request.setParameters(new ArrayOfParameterValue());
    
            SetExecutionParametersResponse response = (SetExecutionParametersResponse) getWebServiceTemplate().marshalSendAndReceive(request,
                    new SoapActionExecutionIdCallback(
                            SET_EXECUTION_PARAMETERS_ACTION,
                            executionId));
        }
    }
    

    Basically the WebServiceGatewaySupport already knows the Marshaller to convert JAXB Pojos. I'm using this one to attach my own header classes to the SoapHeader with this line:

    getMarshaller().marshal(executionHeader, soapMessage.getSoapHeader().getResult());
    

    in my nested WebServiceMessageCallback.

提交回复
热议问题