Spring WS-寻找请求标头/有效负载和响应标头/有效负载示例

时间:2018-10-30 10:56:53

标签: spring web-services soap spring-ws webservicetemplate

我需要使用Spring Framework开发Web Service。 场景是这样的:

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

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

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

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

  • marshalSendAndReceive
  • sendAndReceive

marshalSendAndReceive的问题是尽管我可以发送请求标头,但我不会取回响应标头。

sendAndReceive的问题是尽管我可以提取响应头,但我将无法发送请求头。

当前唯一可用的解决方案是使用拦截器,但这似乎不是处理标头的正确方法,因为标头在到达调用函数之前就被拦截了。为了使调用函数能够访问响应头,我们需要将拦截器设置为有状态的,这是不希望的。

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

使用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;
    }
}

上面的代码始终为响应标头返回null。

1 个答案:

答案 0 :(得分:0)

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

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

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