0

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

Sam
  • 1,298
  • 6
  • 30
  • 65

3 Answers3

3
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;
}
Conffusion
  • 4,335
  • 2
  • 16
  • 28
1

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

Community
  • 1
  • 1
Alex Chernyshev
  • 1,719
  • 9
  • 11
0

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.

mico
  • 12,730
  • 12
  • 59
  • 99
  • wsimport comes with jdk. – mico Jul 26 '14 at 23:04
  • wsimport does not work because the server wsdl do not start with namespace – Sam Jul 27 '14 at 04:31
  • Have you checked the wsdl url content on browser? See everything ok there? Look http://stackoverflow.com/questions/3981873/generating-web-service-classes-using-soapui-with-a-wsdl-over-https if the wsdl seems otherwise ok.. – mico Jul 27 '14 at 19:00