简体   繁体   中英

How to access a SOAP Web Service URL Endpoint from Java

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...

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);
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM