【问题标题】:Not able to get SOAP response after calling external webservice from java从 java 调用外部 Web 服务后无法获得 SOAP 响应
【发布时间】:2014-05-24 22:12:14
【问题描述】:

我有一个从 java 调用外部 web 服务的要求,我将在其中创建 SOAP 请求并传递给 web 服务并返回 SOAP 响应...我通过以下 url :-http://www.concretepage.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 响应...我无法获得响应

【问题讨论】:

    标签: java web-services soap saaj


    【解决方案1】:

    你能不能试试这个来捕捉响应?

    SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    msg.writeTo(out);
    String strMsg = new String(out.toByteArray());
    

    【讨论】:

      猜你喜欢
      • 2015-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-09
      相关资源
      最近更新 更多