如何将SOAP标头添加到Spring Jax-WS客户端?

时间:2010-10-28 18:34:35

标签: java spring jax-ws soapheader

如何将SOAP标头添加到Spring Jax-WS客户端?

具体来说,我有一个Jaxb对象,我想添加到标题,但xml示例将不胜感激。

我正在使用Spring的JaxWsPortProxyFactoryBean描述here。另外,我正在按照here描述生成我的客户端,这比我需要添加的标头更少。

谢谢。

4 个答案:

答案 0 :(得分:15)

更优雅一点(仍需要一个类演员):

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
    }
}

注意:此示例是使用Spring WS 2.1.4进行的测试。

答案 1 :(得分:6)

我仍然试图找到一种优雅的方式来添加标题,但我按照其他人的建议做的是在WebServiceMessageCallBack()上使用Transformer。这是一个示例代码:

JAXBElement<GetDeletedResponse> result = (JAXBElement<GetDeletedResponse>) 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("<account>\n" +
                                "<username>"+"johnsmith"+"</username>\n" +
                                "<password>"+"1234"+"</password>\n" +
                                "</account>");
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.transform(headerSource, header.getResult());

       } catch (Exception e) {
         new RuntimeException(e);
       }
}
...

考虑到这是Spring的WS,它并不是很优雅。这不直观。

答案 2 :(得分:6)

如果找到一个稍微不同的解决方案,经过一番探讨。我正在使用JAXB来编组我的有效负载,并且还使用来自WSDL的JAXB生成了可能的头类。 在我的情况下,我正在解决Microsoft Reporting Services并将ExecutionID作为SOAP标头传递。

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));
    }
}

基本上,WebServiceGatewaySupport已经知道要转换JAXB Pojos的Marshaller。我使用这个将自己的标题类附加到SoapHeader上:

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

在我的嵌套WebServiceMessageCallback中。

答案 3 :(得分:0)

这是另一种解决方案:

public class YourServiceClient extends WebServiceGatewaySupport {

    // custom header inner class
    private final class CustomHeader implements WebServiceMessageCallback {

        private String soapAction;
        private long yourHeaderParameter1;
        private long yourHeaderParameter2;


        public CustomHeader(String soapAction, long yourHeaderParameter1, long yourHeaderParameter2) {
            super();
            if (!StringUtils.hasText(soapAction)) {
                soapAction = "\"\"";
            }
            this.soapAction = soapAction;
            this.yourHeaderParameter1 = yourHeaderParameter1;
            this.yourHeaderParameter2 = yourHeaderParameter2;
        }

        @Override
        public void doWithMessage(WebServiceMessage message) {
            try {
                // get the header from the SOAP message
                SoapHeader soapHeader = ((SoapMessage) message).getSoapHeader();

                // create the header element
                ObjectFactory factory = new ObjectFactory();
                YourGeneratedHeaderEntity header = new YourGeneratedHeaderEntity();
                header.setyourHeaderParameter1(yourHeaderParameter1);
                header.setyourHeaderParameter2(yourHeaderParameter2);

                JAXBElement<YourGeneratedHeaderEntity> headers = factory.createYourGeneratedHeaderEntity(header);

                // create a marshaller
                JAXBContext context = JAXBContext.newInstance(YourGeneratedHeaderEntity.class);
                Marshaller marshaller = context.createMarshaller();

                // set action
                Assert.isInstanceOf(SoapMessage.class, message);
                SoapMessage soapMessage = (SoapMessage) message;
                soapMessage.setSoapAction(soapAction);

                // marshal the headers into the specified result
                marshaller.marshal(headers, soapHeader.getResult());


            } catch (Exception e) {
                logger.error(e.getLocalizedMessage());
            }

        }
    }

    public YourEntityResponse getYourService(long yourHeaderParameter1, long yourHeaderParameter2) {
        GetYourService request = new GetYourService();

        YourEntityResponse response = (YourEntityResponse) getWebServiceTemplate()
                .marshalSendAndReceive(request, new CustomHeader("https://your.service.asmx?WSDL", yourHeaderParameter1, yourHeaderParameter2));

        return response;
    }

}