我使用SOAPMessage.writeTo(OutputStream)来记录 Web 服务消息。一个问题是它也写附件。它占用空间并且二进制附件不可读。有什么方法可以记录没有附件的消息,例如包装器?
一定有比这个更好的解决方案。
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