我正在尝试创建一个soapMessage,以便稍后传入以下代码片段:
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = soapConnectionFactory.createConnection();
SOAPMessage response = connection.call(message, url);
但是,我收到一个空(null)响应:[SOAP-ENV:Body:null]。
当我执行以下操作时(在连接调用之前):
System.out.println(message.getSOAPBody());
message.writeTo(System.out);
当它们应该相同时,我得到两个不同的答案,对吗?
第一个系统打印ln给了我[SOAP-ENV:Body:null],而另一个实际上给了我创建的soap消息(writeTo)。
任何想法为什么?
完整代码:
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory.createMessage();
SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
SOAPElement tvl = envelope.addAttribute(new QName("xmlns:tvl"), "http://some.url.com/");
SOAPBody body = message.getSOAPBody();
QName bodyText = new QName("tvl:searchAirings");
SOAPBodyElement bodyElement = body.addBodyElement(bodyText);
QName fromTag = new QName("from");
SOAPElement from = bodyElement.addChildElement(fromTag);
from.setValue("2012-11-02T14:00:00-4:00");
QName toTag = new QName("to");
SOAPElement to = bodyElement.addChildElement(toTag);
to.setValue("2012-11-02T18:00:00-4:00");
QName networkTag = new QName("network");
SOAPElement network = bodyElement.addChildElement(networkTag);
network.setAttribute("id", "n501");
network.setAttribute("language", "es");
System.out.println(message.getSOAPBody());
message.writeTo(System.out);
答案 0 :(得分:1)
System.out.println(message.getSOAPBody());
=>这应该只打印SOAP信封的主体。
message.writeTo(System.out);
=>这应该打印完整的SOAP消息,即信封,标题和正文。
答案 1 :(得分:0)
您可以通过将响应写入ByteArrayOutputStream
来解决此问题SOAPMessage soapResponse = soapConnection
.call(createSOAPRequest(), url);
ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
soapResponse.writeTo(byteOutStream);
String res = new String(byteOutStream.toByteArray());
System.out.println("Response \n"+res);