ban*_*rCZ 5 java web-services java-ee
我使用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();
Run Code Online (Sandbox Code Playgroud)
示例消息
<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
Run Code Online (Sandbox Code Playgroud)
实际上有一种可能更干净的方法可以做到这一点。
这是我的代码:
// 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();
Run Code Online (Sandbox Code Playgroud)
然后您可以记录 xmlString,它仅对应于 Envelope 部分。
| 归档时间: |
|
| 查看次数: |
1979 次 |
| 最近记录: |