0

How to access a SOAP Web Service URL Endpoint from Java?

I've been searching for a concrete example for a while and I'm getting no-where. Unfortunately my preferred method using REST isn't available in this scenario I'm working on, so having to test a SOAP based approach.

The setup is, there is a SOAP WSDL file located at www.website.com/soap.wsdl for example. And I'm trying to send and receive data from a CreateData endpoint.

When I add the WSDL file into SoapUI software to test, this generates the following SOAP message template;

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tic="{redacted}">
   <soapenv:Header/>
   <soapenv:Body>
      <tic:CreateData>
         <tic:request>{"testKey":"testValue"}</tic:request>
      </tic:CreateData>
   </soapenv:Body>
</soapenv:Envelope>

(I added the test JSON data)

What I can't seem to figure out is how to turn this request which works correctly in SoapUI into a functioning piece of test Java code.

When I import the WSDL into my IDE, it generates a single Java file which looks like this;

import javax.jws.WebService;

@WebService(serviceName = "{redacted}", portName = "{redacted}", endpointInterface = "{redacted}", targetNamespace = "{redacted}", wsdlLocation = "WEB-INF/wsdl/null/null.wsdl")
public class NewWebServiceFromWSDL {

    public java.lang.String createData(java.lang.String request) {
        //TODO implement this method
        throw new UnsupportedOperationException("Not implemented yet.");
    }

}

Naturally, I now have no idea what to do next. How do I go about creating a SOAP Envelope in Java? I would have expected to see some methods generated for things such as setSOAPEnvelopeHeader(), setSOAPEnvelopeBody(), setSOAPEnvelopeVariableRequest() etc. (or similar).

Then once the SOAP Envelope object has been created (however that is done), how do I then go about actually sending the message and handling the response?

Feeling confused...

Red Boy
  • 5,429
  • 3
  • 28
  • 41
Michael Cropper
  • 872
  • 1
  • 10
  • 28
  • You don't create the SOAP requests, your client library should do that all for you. You should be able to "import" the wsdl, and then call the functions as if they were simply java functions... although if you're mocking it I guess this answer is really what you want:https://stackoverflow.com/questions/15948927/working-soap-client-example See also https://stackoverflow.com/questions/18869686/how-to-consume-a-soap-web-service-in-java?noredirect=1&lq=1 – Cody G May 04 '18 at 17:10

1 Answers1

0

This is cruel way of calling SOAP, but it works, and will be good to get going. Then, you do some modifications to deal with SOAPEnvelope/SOAPBody and that's all.

package xml;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;

import com.sun.xml.internal.ws.encoding.soap.SOAP12Constants;


public class SOAPCall {

public static void main(String[] args) throws UnsupportedOperationException, SOAPException, IOException {

    String endPoint = "URL";

    SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
    SOAPConnection soapConnection = soapConnectionFactory.createConnection();

    String soapString = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tic=\"{redacted}\">"
            + "<soapenv:Header/><soapenv:Body>"
            + "<tic:CreateData><tic:request>{\"testKey\":\"testValue\"}</tic:request>"
            + "</tic:CreateData></soapenv:Body></soapenv:Envelope>";

    InputStream is = new ByteArrayInputStream(soapString.getBytes());

    SOAPMessage request = MessageFactory.newInstance().createMessage(null, is);

    MimeHeaders headers = request.getMimeHeaders();
    // If SOAP Header is compulsory
    // headers.addHeader("SOAPAction",
    // "http://www.tdcare.com/DataInterchange");
    headers.setHeader("Content-Type", "application/xml");
    request.saveChanges();

    SOAPMessage soapResponse = soapConnection.call(request, endPoint);

    System.out.println(soapResponse);
}
}
Red Boy
  • 5,429
  • 3
  • 28
  • 41