我需要从java调用外部Web服务,在那里我将创建SOAP请求并传递给Web服务并将获得SOAP响应...我浏览了以下URL: - {{3实现它...我的 SOAP请求如下: -
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://services.test.com/schema/MainData/V1">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<v1:retrieveDataRequest>
<v1:Id>22</v1:Id>
</v1:retrieveDataRequest>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
我的 SOAP响应是:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<retrieveDataResponse xmlns="http://services.test.com/schema/MainData/V1">
<Response>The Data retrieved from the Database</Response>
<Id>21</Id>
<Name>fdfdf</Name>
<Age>44</Age>
<Designation>dgdgdfg</Designation>
</retrieveDataResponse>
</soap:Body>
</soap:Envelope>
我的 Java代码是: -
import javax.xml.soap.*;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
public class Main2
{
/**
* Method used to create the SOAP Request
*/
private static SOAPMessage createSOAPRequest() throws Exception
{
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
/*
Construct SOAP Request Message:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://services.test.com/schema/MainData/V1">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<v1:retrieveDataRequest>
<v1:Id>21</v1:Id>
</v1:retrieveDataRequest>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
*/
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("v1", "http://services.test.com/schema/MainData/V1");
// SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyElem = soapBody.addChildElement("retrieveDataRequest", "v1");
SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("Id", "v1");
soapBodyElem1.addTextNode("21");
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", "http://services.test.com/schema/MainData/V1" + "http://services.test.com/schema/MainData/V1/retrieveDataOperation");
soapMessage.saveChanges();
// Check the input
System.out.println("Request SOAP Message = ");
soapMessage.writeTo(System.out);
System.out.println();
return soapMessage;
}
/**
* Method used to print the SOAP Response
*/
private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception
{
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
Source sourceContent = soapResponse.getSOAPPart().getContent();
System.out.println("\nResponse SOAP Message = ");
StreamResult result = new StreamResult(System.out);
transformer.transform(sourceContent, result);
}
/**
* Starting point for the SAAJ - SOAP Client Testing
*/
public static void main(String args[])
{
try
{
// Create SOAP Connection
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
//Send SOAP Message to SOAP Server
String url = "http://localhost:8082/mainData?wsdl";
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);
// Process the SOAP Response
printSOAPResponse(soapResponse);
soapConnection.close();
}
catch (Exception e)
{
System.err.println("Error occurred while sending SOAP Request to Server");
e.printStackTrace();
}
}
}
现在我的问题是我在控制台中打印 SOAP请求,但我没有从服务中获取 SOAP响应 ...请帮助...如何在控制台中打印SOAP响应...我无法得到响应
答案 0 :(得分:0)
你能不能尝试这个来捕捉回应?
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);
ByteArrayOutputStream out = new ByteArrayOutputStream();
msg.writeTo(out);
String strMsg = new String(out.toByteArray());