Web Service Client

Web Service Client

In the previous tutorial we learned how to create the JAX-WS Web Service. If you have not visited the tutorial please click this link Web Service Host.
After the Webservice host has hosted the service on a Web Server it time to call the Service. First we need the wsdl file. Since the webservice is hosted we can get the wsdl wasily. Go to browser and hit below link

http://localhost:8080/webservicehost/HelloWs?wsdl
The browser will return a xml text that looks like below give xml

<?xml version="1.0" encoding="UTF-8"?><!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. 
 RI's version is JAX-WS RI 2.1.3-b02-. --><!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. 
 RI's version is JAX-WS RI 2.1.3-b02-. -->
<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
 xmlns:tns="http://webservicehost.webservicehost/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://webservicehost.webservicehost/"
 name="HelloWsService">
 <types>
  <xsd:schema>
   <xsd:import namespace="http://webservicehost.webservicehost/"
    schemaLocation="HelloWs.xsd"></xsd:import>
  </xsd:schema>
 </types>
 <message name="sayHello">
  <part name="arg0" type="xsd:string"></part>
 </message>
 <message name="sayHelloResponse">
  <part name="return" type="tns:return"></part>
 </message>
 <portType name="HelloWs">
  <operation name="sayHello">
   <input message="tns:sayHello"></input>
   <output message="tns:sayHelloResponse"></output>
  </operation>
 </portType>
 <binding name="HelloWsPortBinding" type="tns:HelloWs">
  <soap:binding transport="http://schemas.xmlsoap.org/soap/http"
   style="rpc"></soap:binding>
  <operation name="sayHello">
   <soap:operation soapAction=""></soap:operation>
   <input>
    <soap:body use="literal" namespace="http://webservicehost.webservicehost/"></soap:body>
   </input>
   <output>
    <soap:body use="literal" namespace="http://webservicehost.webservicehost/"></soap:body>
   </output>
  </operation>
 </binding>
 <service name="HelloWsService">
  <port name="HelloWsPort" binding="tns:HelloWsPortBinding">
   <soap:address location="http://localhost:8080/webservicehost/HelloWs"></soap:address>
  </port>
 </service>
</definitions>

Save the file as hellowWs.wsdl. Now we have the WSDL file of the Web Service. What we need to do is create the client, for this JDK provides a tool called wsimport short form for Web Service Import. We can use wsimport from command line and provide the saved WSDL location as a parameter as given below.

wsimport -keep helloWs.wsdl

This command will generate java files as mentioned below:
HelloWs.class
HelloWsService.class
ObjectFactory.class
package-info.class
Return.class

After these files are created create a simple stand alone application with main(String args[]) method.

Keep the files generated by wsimport in the class path of this standalone application.

public class MyClass {
    
    public static void main(String arg[]){
        
        System.out.println("hi");
        
        HelloWsService helloWsService = new HelloWsService();
        HelloWs helloWs = helloWsService.getHelloWsPort();
        System.out.println(helloWs.sayHello("abc").getCode());
    }

}
The output we will get is:
hi
Hello! abc

SOAP UI Client
As i mentioned before that the actual request and response takes place through SOAP XML ,we can generate the SOAP xml if we try to test the deployed Web Service through SOAP UI application. SOAP UI application creates a request SOAP XML and sends it to the mentioned endpoint over HTTP.
Lets see how the actual SOAP xml request looks

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webservicehost.webservicehost/">
   <soapenv:Header/>
   <soapenv:Body>
      <web:sayHello>
         <arg0>John Doe</arg0>
      </web:sayHello>
   </soapenv:Body>
</soapenv:Envelope>

And after submitting this request on http://localhost:8080/webservicehost/HelloWs the response is

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns2:sayHelloResponse xmlns:ns2="http://webservicehost.webservicehost/">
         <return>
            <code>Hello! John Doe</code>
         </return>
      </ns2:sayHelloResponse>
   </S:Body>
</S:Envelope>

The Java Web service client that we had created above using wsimport generates the same SOAP XML request as shown above and send it over HTTP. The response from JAX-WS Web Service is also a SOAP XML as shown above.



For Web Service Introduction click here

Below are some posts that explain how to implement and test SOAP/REST Webservices

Host
SOAP REST
JAX-WS JAX-RS
Spring-ws Spring-MVC-REST
Client
SOAP REST
JAX-WS(wsimport) Google REST APP
SOAP UI Apache REST

No comments:

Post a comment

Share the post