[英]How to get request and response with Axis2?
我有一個用Axis2生成的SOAP客戶端。 它使用JAXB-RI,盡管那可能並不重要。
我沒有任何XML配置。 只需獲取由wsdl2java
生成的Stub
,准備響應(作為POJO)並在Stub
上執行方法。
如何獲取在此調用上交換的請求和響應的原始XML(作為String)?
我想出了service._getServiceClient().getLastOperationContext().getMessageContext("Out").getEnvelope()
(與"In"
類似),但是其中一個引發異常,因為流已經被處理了。
聽起來很明顯,但是我不知道怎么做,而且官方文檔非常少。
不知道“ In”消息標簽有什么問題,
但是在搜索時發現以下JIRA票證https://issues.apache.org/jira/browse/AXIS2-5469 ,指向https://issues.apache.org/jira/browse/AXIS2-5202 ,在討論中發現了一個WA使用以下代碼解決了此問題,我能夠偵聽soap請求的Response消息。
stub._getServiceClient().getAxisService().addMessageContextListener(
new MessageContextListener() {
public void attachServiceContextEvent(ServiceContext sc,
MessageContext mc) {}
public void attachEnvelopeEvent(MessageContext mc) {
try
{ mc.getEnvelope().cloneOMElement().serialize(System.out); }
catch (XMLStreamException e) {}
}
});
我遇到了類似的問題,您可以從下面的代碼中獲取參考
private void ResponseSOAPMessage(OperationContext opCtx, OMElement responseOM)
throws AxisFault
{
MessageContext msgCtxIn = opCtx.getMessageContext("In");
if (!msgCtxIn.getEnvelope().isComplete())
{
msgCtxIn.getEnvelope().getBody().getFirstOMChild().close(false);
msgCtxIn.getEnvelope().getBody().addChild(responseOM);
msgCtxIn.getEnvelope().getBody().getFirstOMChild().detach();
}
CommonsTransportHeaders inHeaders = (CommonsTransportHeaders)msgCtxIn.getProperty("TRANSPORT_HEADERS");
if (msgCtxIn.getEnvelope().isComplete())
{
System.out.println("SOAP Response:");
System.out.println(msgCtxIn.getEnvelope().toString());
}
else
{
System.out.println("SOAP Response Message Body:");
System.out.println(responseOM.toString());
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.