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

我有一个需要与之交谈的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令牌,因此对正在发生的事情一无所知.有任何想法吗?

解决方法:

我是个白痴. ‘是错字.我需要打电话给:

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标头的回调:
        类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());
上一篇:微信小程序 - loading(组件)


下一篇:基于Flask的web微信