java 记录 SOAP 消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6421627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Log SOAP Messages
提问by Mark Estrada
I created a class that intercepts the Request-Response cycle of SOAP message exchange and I wanted to log the exchange of messages. What's the best way so that I could log the SOAP message in my log file?
我创建了一个类来拦截 SOAP 消息交换的请求-响应循环,我想记录消息的交换。什么是最好的方法,以便我可以在我的日志文件中记录 SOAP 消息?
I dont want it to be pretty printed in my log file but I just want to access and view the request and response SOAP envelope.
我不希望它在我的日志文件中打印出来,但我只想访问和查看请求和响应 SOAP 信封。
I tried with this code:
我尝试使用此代码:
public class LogHandler{
private static final Logger _LOG;
@Override
protected void handleResponse(SOAPMessage message)
logSOAPMessage(message);
}
@Override
protected void handleRequest(SOAPMessage message)
logSOAPMessage(message);
}
private void logSOAPMessage(SOAPMessage message){
_LOG.info(":: Logging SOAP Message :: " + message.toString());
}
}
But doesnt get the required message.
但没有得到所需的消息。
:: Logging SOAP Message :: oracle.j2ee.ws.saaj.soap.soap11.Message11@715346
Any hints?
任何提示?
采纳答案by rajasaur
What you are seeing is the toString of the SOAPMessage. You might have to lookup the javadocs to see if you can get the SOAPEnvelope from Oracle's SOAPMessage and that should contain the incoming/outgoing SOAP message.
您看到的是 SOAPMessage 的 toString。您可能需要查找 javadoc 以查看是否可以从 Oracle 的 SOAPMessage 获取 SOAPEnvelope,并且该信封应该包含传入/传出 SOAP 消息。
Edit: Please check thisfor getSoapHeader() and getSoapBody() to deconstruct the soap message. You might have to figure out the same for Oracle's wrapper of SOAPMessage.
编辑:请检查此为getSoapHeader()和getSoapBody()来解构SOAP消息。您可能必须为 Oracle 的 SOAPMessage 包装器找出相同的方法。
回答by Cratylus
If message
is the SOAPMessage
then do the following:
如果message
是,SOAPMessage
则执行以下操作:
ByteArrayOutputStream bout = new ByteArrayOutputStream();
message.writeTo(bout);
String msg = bout.toString("UTF-8");
Now String msg
has the soap message and you can log it.
现在 Stringmsg
有肥皂消息,您可以记录它。
In your example:
在你的例子中:
private void logSOAPMessage(SOAPMessage message){
ByteArrayOutputStream bout = new ByteArrayOutputStream();
message.writeTo(bout);
String msg = bout.toString("UTF-8");
_LOG.info(":: Logging SOAP Message :: " + msg);
}
回答by Lyth
Cratylus' answer is insufficient when you don't want to log SOAP attachments.
当您不想记录 SOAP 附件时,Cratylus 的回答是不够的。
Here's a way to log only the Envelope :
这是一种仅记录 Envelope 的方法:
// Get the Envelope Source
Source src = message.getSOAPPart().getContent() ;
// Transform the Source into a StreamResult to get the XML
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "no");
StreamResult result = new StreamResult(new StringWriter());
transformer.transform(src, result);
String xmlString = result.getWriter().toString();