没有附件的SOAPMessage writeTo [英] SOAPMessage writeTo without attachment
本文介绍了没有附件的SOAPMessage writeTo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I use SOAPMessage.writeTo(OutputStream) to log web service messages. A problem is that it also writes attachments. It is space consuming and the binary attachments are not readable . Is there any way how to log the message without attachments, e.g. a wrapper?
必须有比这更好的解决方案.
There must be a better solution than this one.
ByteArrayOutputStream out = new ByteArrayOutputStream();
message.writeTo(out);
StringBuilder builder = new StringBuilder(out.toString());
int indexOfAttachment = builder.indexOf("------=");
if (indexOfAttachment != -1) {
return builder.substring(0, indexOfAttachment);
}
return builder.toString();
示例消息
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Header />
<S:Body>
<ns2:wsGetObjectByIDResponse
xmlns:ns2="http://xxx.com/"
xmlns:ns3="http://yyy.com/">
<return>
<serviceResponse status="OK" />
<contentData formatName="jpeg_lres"
objectContent="cid:e677f02c-002a-4c2c-8fd9-a3acdba5ad11@example.jaxws.sun.com"
objectName="Smlouva1.jpg" />
</return>
</ns2:wsGetObjectByIDResponse>
</S:Body>
</S:Envelope>
------=_Part_9_-806948376.1352979403086
Content-Type: image/jpeg
Content-ID: <e677f02c-002a-4c2c-8fd9-a3acdba5ad11@example.jaxws.sun.com>
Content-Transfer-Encoding: binary
����\x00JFIF\x00\x00�\x00�\x00\x00��\x00C\x00
推荐答案
实际上,有一种方法可能更清洁.
There's actually a way to do this that is probably cleaner.
这是我的代码:
// 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();
然后您可以记录xmlString,它仅对应于Envelope部分.
Then you can log the xmlString, it corresponds to the Envelope part only.
这篇关于没有附件的SOAPMessage writeTo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文