[英]Not able to get SOAP response after calling external webservice from java
我有一个从java调用外部Web服务的要求,在该处我将创建SOAP请求并传递给Web服务,并将获得SOAP响应……我经历了以下URL: -http:// www。为了达到目的, 具体页面 .com / webservices / java-saaj-web-service-example ...我的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响应...我无法得到回应
您不仅可以尝试这样做来捕获响应吗?
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);
ByteArrayOutputStream out = new ByteArrayOutputStream();
msg.writeTo(out);
String strMsg = new String(out.toByteArray());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.