0

When I print the response in createSOAPResponse, I do get the correct response body but I do not get actual values. I'll get a node element like this <xsd:element name="Incident_Number" type="xsd:string"/> but no associated value. I am new to this. Am I missing any steps?

I've included the entire source code of my class for clarity:

 public class RemedySOAPService {

    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {

        // Establish SOAP Connection
        SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
        SOAPConnection soapConnection = soapConnectionFactory.createConnection();

        // Create SOAP Request
        String namespaceURI = "XXX";
        SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), namespaceURI);

        // Create SOAP Response
        createSOAPResponse(soapResponse);

        // Close Connection
        soapConnection.close();

    }

    /**
     * @param soapResponse
     * @throws Exception
     */
    private static void createSOAPResponse(SOAPMessage soapResponse) throws Exception {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        Source sourceContent = soapResponse.getSOAPPart().getContent();
        System.out.println("SOAP Response:");
        StreamResult result = new StreamResult(System.out);
        transformer.transform(sourceContent, result);
    }

    /**
     * @return soapMessage
     * @throws Exception
     */
    private static SOAPMessage createSOAPRequest() throws Exception {
        MessageFactory messageFactory = MessageFactory.newInstance();

        // Create SOAP Message
        SOAPMessage soapMessage = messageFactory.createMessage();

        /*<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="XXX">
           <soapenv:Header>
              <urn:AuthenticationInfo>
                 <urn:userName>XXX</urn:userName>
                 <urn:password>XXX</urn:password>

              </urn:AuthenticationInfo>
           </soapenv:Header>
           <soapenv:Body>
              <urn:HelpDesk_QueryList_Service>
                 <urn:Qualification>XXX</urn:Qualification>
              </urn:HelpDesk_QueryList_Service>
           </soapenv:Body>
        </soapenv:Envelope>*/

        // Create SOAP Part
        SOAPPart soapPart = soapMessage.getSOAPPart();

        // Set Constants
        String xmlns = "XXX";
        String username = "XXX";
        String password = "XXX";
        String qualification = "XXX";

        // Create SOAP Envelope
        SOAPEnvelope envelope = soapPart.getEnvelope();
        envelope.addNamespaceDeclaration("urn", xmlns);

        // Create SOAP Header
        SOAPHeader soapHeader = envelope.getHeader();

        // Create SOAP Element for AuthenticationInfo
        SOAPElement authInfoElem = soapHeader.addChildElement("AuthenticationInfo", "urn");

        // Create SOAP ChildElements for userName and password
        SOAPElement usernameChild = authInfoElem.addChildElement("userName", "urn");
        SOAPElement passwordChild = authInfoElem.addChildElement("password", "urn");

        // Assign values to SOAP ChildElements
        usernameChild.addTextNode(username);
        passwordChild.addTextNode(password);

        // Create SOAP Body
        SOAPBody soapBody = envelope.getBody();
        SOAPElement bodyElem = soapBody.addChildElement("HelpDesk_QueryList_Service", "urn");
        createElementAndSetText(bodyElem, "Qualification", qualification);

        // Create MimeHeaders
        String namespaceURI = "XXX";
        MimeHeaders headers = soapMessage.getMimeHeaders();
        headers.addHeader("SOAPAction", namespaceURI + "/XXX");

        // Save Request
        soapMessage.saveChanges();

        // Print the Request Message
        printSOAPRequest(soapMessage);

        return soapMessage;
    }

    /**
     * @param parent
     * @param element
     * @param text
     * @return
     * @throws SOAPException
     */
    private static SOAPElement createElementAndSetText(SOAPElement parent, String element, String text)
            throws SOAPException {
        SOAPElement child = parent.addChildElement(element, "urn");
        child.addTextNode(text);

        return child;
    }

    /**
     * @param soapMessage2
     * @throws SOAPException
     * @throws IOException
     */
    private static void printSOAPRequest(SOAPMessage soapMessage) throws SOAPException, IOException {
        System.out.print("SOAP Request:");
        System.out.println();
        soapMessage.writeTo(System.out);
        System.out.println();
        System.out.println();
    }

}

My goal is to collect a list of returned from the SOAP response and use them in a second operation. But how can I extract the values from that element if they don't appear?

I have tested in SoapUI so I'm sure that the requests and responses are correct. I'm just having trouble building the client.

Martin Erlic
  • 5,467
  • 22
  • 81
  • 153
  • http://stackoverflow.com/questions/522395/getting-raw-xml-from-soapmessage-in-java – Karthik Nov 09 '16 at 13:23
  • It looks like you're getting the WSDL rather than an actual response. Could you include some more details of the web service URI you're using, anonymised if necessary? – Owen Nov 16 '16 at 03:09

0 Answers0