SOAPMessage writeTo方法不带附件

5
我使用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

是的,有点遗憾没有更好的方法... - Adrien Gorrell
不仅附件在我的情况下无法读取,而且流已被消耗,后来在实际提取时访问流时只得到了-1...因此最好避免阅读/打印任何附件。 - Lonzak
2个回答

4

实际上,有一种更加简洁的方法来实现这个功能。

以下是我的代码:

// 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,它仅对应于信封部分。

0

二进制附件在信封之后,详见我编辑过的问题。 - banterCZ
你不能使用像AXIOM这样的库来创建SOAP消息并打印它吗? - andunslg
我只想修剪二进制数据。我期望更好的SOAPMessage API。 - banterCZ

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接