简体   繁体   中英

Webservice client to invoke a webservice client - java

I have to create a java client to execute a webservice . I have a XML containing the whole SOAP Request (Envelope , Header, Body ) .

How do i write a java code to execute the webservice , by passing xml file which contains soap request ?

I tried searching a lot but could not find a sample that does this

The webservice on the server is on SOAP 1.1 with content-Type 'text/xml'

For Example wsdlLocation="http://localhost:8080/helloservice/hello?wsdl"

The webservice does not have a input parameter , that's why the data has to be passed completely as a soap request. The data that is passed is in the form of xml.

Sample SOAP Request xml file sample (Sample.xml)

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Body>
<putTxlife1203Info xmlns="http://www.openuri.org/">
<TXLife>

</TXLife>
</putTxlife1203Info>
</env:Body>
</env:Envelope>

It would be really great if somebody could provide a sample it would be great

import javax.xml.soap.*;

public String callTestService(String soapRequestXml, String url) throws Exception {
    // Create SOAP Connection
    SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
    SOAPConnection soapConnection = soapConnectionFactory.createConnection();

    SOAPMessage soapRequest = MessageFactory.newInstance().createMessage(new MimeHeaders(),
            new ByteArrayInputStream(soapRequestXml.getBytes()));

    // Send SOAP Message to SOAP Server
    SOAPMessage soapResponse = soapConnection.call(soapRequest, url);

    ByteArrayOutputStream soapResponseBaos = new ByteArrayOutputStream();
    soapResponse.writeTo(soapResponseBaos);
    String soapResponseXml = soapResponseBaos.toString();

    return soapResponseXml;
}

If you have full xml with SOAP request saved in file and need to send it directly (for testing as I guess) then just use plain http client and do POST request with it. Here is some samples of how to do it:

Sending HTTP Post request with SOAP action using org.apache.http

To generate the client side service methods and stuff, please use wsimport tool as follows:

wsimport -keep http://localhost:8080/helloservice/hello?wsdl

Source: http://www.mkyong.com/webservices/jax-ws/jax-ws-hello-world-example/ and there, if you really (I mean REALLY REALLY) want to do it all by hand, the instructions are also there. I would prefer generating over creating things so, let's keep it simple.

Then you create the client with something like:

package com.mkyong.client;

import com.mkyong.ws.HelloWorld;
import com.mkyong.ws.HelloWorldImplService;

public class HelloWorldClient{

    public static void main(String[] args) {

    HelloWorldImplService helloService = new HelloWorldImplService();
    HelloWorld hello = helloService.getHelloWorldImplPort();

    System.out.println(hello.getHelloWorldAsString("mkyong"));

   }

}

That was a direct quote from the link above, and the method names may vary depending which tutorial you followed for the actual service on server side.

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