Spring ClientInterceptor在响应中显示空SOAP标头

时间:2014-11-11 14:40:17

标签: java spring web-services soap interceptor

我有一个需要与之交谈的SOAP服务。第一个SOAP请求在SOAP标头中接收带有 sessionID 元素的响应,我需要在最终发布命令中发送该元素。

要获取此sessionID值,我计划使用 ClientInterceptor 。在我的 WebServiceGatewaySupport 的实现中,我注册了我的拦截器:

this.setInterceptors(new ClientInterceptor[] { new MyWebServiceClientInterceptor() });

我的拦截器:

public class MyWebServiceClientInterceptor implements ClientInterceptor {

    public final QName SessionID_QNAME = new QName("http://xml.example.com/ws/session", "sessionID");

    public boolean handleFault(MessageContext context) throws WebServiceClientException {
        logger.info("Handle Fault");
        return true;
    }       

    public boolean handleResponse(MessageContext context) throws WebServiceClientException {
        logger.info("Handle Response");
        SoapMessage soapMessage = (SoapMessage) context.getRequest();
        SoapHeader soapHeader = soapMessage.getSoapHeader();

        logger.info("Response Header: " + soapHeader.getName());
        Iterator<SoapHeaderElement> qn = soapHeader.examineAllHeaderElements();
        while (qn.hasNext()) {
            SoapElement elem = qn.next();
            logger.info(elem.toString());
        }
        Iterator<QName> an = soapHeader.getAllAttributes();
        while (an.hasNext()) {
            QName elem = an.next();
            logger.info(elem.toString());
        }
        return true;
    }

    public boolean handleRequest(MessageContext context) throws WebServiceClientException {
        logger.info("Handle Request");
        return true;
    }
}

但是我不断回到空的SOAP标头。我正在运行Wireshark,并且可以在返回的SOAP数据包中清楚地看到 sessionID 令牌,这样就不知道发生了什么。有什么想法吗?

1 个答案:

答案 0 :(得分:4)

我是个白痴。 &#39; Twas是一个错字。我需要打电话:

SoapMessage soapMessage = (SoapMessage) context.getResponse();

SoapMessage soapMessage = (SoapMessage) context.getRequest();

那些想要在请求上设置标头并在响应中接收标头的人的完整代码:

ClientInterceptor接收自定义SOAP标头值:

public class MyWebServiceClientInterceptor implements ClientInterceptor {

    public boolean handleFault(MessageContext context) throws WebServiceClientException {
        return true;
    }

    public boolean handleResponse(MessageContext context) throws WebServiceClientException {
        SoapMessage soapMessage = (SoapMessage) context.getResponse();
        SoapHeader soapHeader = soapMessage.getSoapHeader();

        Iterator<SoapHeaderElement> qn = soapHeader.examineHeaderElements(MY_SESSIONID_QNAME);

        while (qn.hasNext()) {
            SoapElement elem = qn.next();
            SoapHeaderElement headerElem = (SoapHeaderElement) elem;
            if (StringUtil.validString(headerElem.getText())) {
                if (!headerElem.getText().equals(sessionId)) {
                    sessionId = headerElem.getText();
                    logger.info("Bound to Session ID: " + sessionId);
                }
            }
        }
        return true;
    }

    public boolean handleRequest(MessageContext context) throws WebServiceClientException {
        return true;
    }
}

我使用拦截器的地方:

public class MySoapClient extends WebServiceGatewaySupport {

public MySoapClient() {
    this.setInterceptors(new ClientInterceptor[] { new MyWebServiceClientInterceptor() });
    ...
}

...
}

回调以设置自定义SOAP标头:         class MySoapActionCallback实现了WebServiceMessageCallback {

    public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException {
        SaajSoapMessage soapMessage = (SaajSoapMessage) message;
        SoapHeaderElement messageId = soapMessage.getSoapHeader().addHeaderElement(SessionID_QNAME);
        if (StringUtil.validString(sessionId)) {
            messageId.setText(sessionId);
        }
    }
}

我使用回调的地方:

JAXBElement<ReturnType> result = (JAXBElement<ReturnType>) getWebServiceTemplate().marshalSendAndReceive(of.createRelease(null), new MySoapActionCallback());