Spring WS - 寻找请求 header/payload 和响应 header/payload 示例

Spring WS - Looking for Request header/payload and Response header/payload Example

我需要使用 Spring 框架开发 Web 服务。 场景是这样的:

我的程序将向服务发送 SOAP 请求(header + 有效负载)并期望从服务返回响应(header + 有效负载)。 有效负载和 header 都很重要,也是程序所需要的。

问题是我无法在 Spring WS 中找到任何示例,其中 header 和有效负载都作为请求的一部分发送,而 header 和从响应中提取的有效负载。

我正在使用 WebServiceGatewaySupportWebServiceTemplate 发送请求和获取响应。

WebServiceTemplate提供了2种发送请求的方法:

marshalSendAndReceive 的问题是我不会收到响应 header 尽管我可以发送请求 header.

sendAndReceive 的问题是我将无法发送请求 header 尽管我将能够提取响应 header.

目前唯一可用的解决方案是使用拦截器,但似乎这不是处理 header 的正确方法,因为 header 在到达调用函数之前被拦截。要让调用函数访问响应 header,我们需要将拦截器设为有状态,这是不可取的。

我将非常感谢任何可以为我提供如何正确实现此目标的示例的指导和帮助。

使用 sendAndReceive 时请在下面找到我的代码:

public class ClientAccountInformation extends WebServiceGatewaySupport {

    public ClientAccountInformation() {
    }

    public FIGetAcctsInfoCallBackRs sendRequest(GetAcctInfoRq request, HeaderRq headerRq) {

        WebServiceTemplate webServiceTemplate =  getWebServiceTemplate();
        try {
            ResponseAndHeader responseAndHeader = webServiceTemplate.sendAndReceive(Constants.WEBSERVICE_URL, 
                new WebServiceMessageCallback() {
                  public void doWithMessage(WebServiceMessage message) throws IOException {
                      try {
                          Jaxb2Marshaller marshallerRq = new Jaxb2Marshaller();
                          marshallerRq.setContextPath("com.abc.domain..getacctinfo");

                          marshallerRq.afterPropertiesSet();

                          MarshallingUtils.marshal(marshallerRq, request, message);

                          SoapHeader soapHeader = ((SoapMessage)message).getSoapHeader();
                          JAXBContext context = JAXBContext.newInstance(HeaderRq.class);
                          Marshaller marshaller = context.createMarshaller();
                          marshaller.marshal(HeaderRq, soapHeader.getResult());
                      } catch (Exception e) {
                          e.printStackTrace();
                      }
                  }
                },
                new WebServiceMessageExtractor<ResponseAndHeader>() {
                  public ResponseAndHeader extractData(WebServiceMessage message) throws IOException {
                    SoapHeader header = ((SoapMessage)message).getSoapHeader();
                    Iterator<SoapHeaderElement> it = header.examineHeaderElements(new QName("urn:test", "HeaderRs"));
                    return new ResponseAndHeader(
                        it.hasNext() ? (HeaderRs)jaxb2Marshaller().unmarshal(it.next().getSource())
                                     : null,
                        (GetAcctInfoRs) MarshallingUtils.unmarshal(jaxb2Marshaller(), message));
                  }
                });

            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    Jaxb2Marshaller jaxb2Marshaller() {
        Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
        jaxb2Marshaller.setContextPath("com.abc.domain.getacctinfo");

        return jaxb2Marshaller;
    }
}

以上代码总是 returns 响应为 null header。

我已经解决了这个问题。我可以将 SOAP header 发送到服务并从响应中提取响应 header。不需要拦截器。

主要帮助来自 Andreas Veithen 的博客 post。谢谢

public class ClientSamaAccountInformation extends WebServiceGatewaySupport {

    public ClientSamaAccountInformation() {
    }

    public FIGetAcctsInfoCallBackRs sendRequest(FIGetAcctsInfoCallBackRq request, MsgHdrRq msgHdrRq) {

        WebServiceTemplate webServiceTemplate =  getWebServiceTemplate();
        try {
            ResponseAndHeader responseAndHeader = webServiceTemplate.sendAndReceive(Constants.WEBSERVICE_URL, 
                new WebServiceMessageCallback() {
                  public void doWithMessage(WebServiceMessage message) throws IOException {
                      try {
                          Jaxb2Marshaller marshallerRq = new Jaxb2Marshaller();
                          marshallerRq.setContextPath("com.abc.domain..getacctinfo");

                          marshallerRq.afterPropertiesSet();

                          MarshallingUtils.marshal(marshallerRq, request, message);

                          SoapHeader soapHeader = ((SoapMessage)message).getSoapHeader();
                          JAXBContext context = JAXBContext.newInstance(MsgHdrRq.class);
                          Marshaller marshaller = context.createMarshaller();
                          marshaller.marshal(msgHdrRq, soapHeader.getResult());
                      } catch (Exception e) {
                          e.printStackTrace();
                      }
                  }
                },
                new WebServiceMessageExtractor<ResponseAndHeader>() {
                  public ResponseAndHeader extractData(WebServiceMessage message) throws IOException {

                    //Extract response payload
                    FIGetAcctsInfoCallBackRs response = null;
                    try {
                        Jaxb2Marshaller marshallerRs = new Jaxb2Marshaller();
                        marshallerRs.setContextPath("com.abc.domain..getacctinfo");
                        marshallerRs.afterPropertiesSet();
                        response = (FIGetAcctsInfoCallBackRs) MarshallingUtils.unmarshal(marshallerRs, message);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    //Extract response header
                    MsgHdrRs msgHdrRs = null;
                    try {
                        JAXBContext context = JAXBContext.newInstance(MsgHdrRs.class);
                        Unmarshaller unmarshaller = context.createUnmarshaller();

                        SoapHeader header = ((SoapMessage)message).getSoapHeader();
                        Iterator<SoapHeaderElement> it = header.examineHeaderElements(new QName("http://www.abc.def.com/common/Header", "MsgHdrRs"));
                        while(it.hasNext()) {
                            msgHdrRs = (MsgHdrRs) unmarshaller.unmarshal(it.next().getSource());
                            System.out.println(msgHdrRs);

                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    return null;
                  }
                });

            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}