Spring WS-查找请求头/负载和响应头/负载示例



我需要使用Spring Framework开发Web服务。场景如下:

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

问题是,我在SpringWS中找不到任何一个例子,其中头和有效负载都是作为请求的一部分发送的,头和有效载荷是从响应中提取的。

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

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

  • marshalSendAndReceive
  • sendAndReceive

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

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

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

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

使用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。

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

主要帮助来自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;
}
}
}

相关内容

  • 没有找到相关文章

最新更新