May 22, 2015

Java Web Service Automatic Client Generation with WSImport


If you have access to the WSDL file of a web service, that contains informations like available methods, kind of parameters and what will be returned, then you are able to use the web service. Due to this public and well formed information, it's possible to automate the client generation! And that's the point of the wsimport tool, here you can see how.

If you don't know some term or technology used in the post, click here. The tool will read the WSDL file from the web service URL and then generate the Java classes files that will be used to access the web service methods. 

Start downloading the ws-import tool here, extract it and go to the bin folder "\jaxws-ri\bin", there is the "wsimport.bat". Now run the Calculator web service, open a terminal, go to the bin folder, run it with the following syntax "wsimport.bat -keep <WSDL URL> -d <output dir>" and you should see something like this:

wsimport -keep http://localhost:8080/ws_soap/calc?wsdl -d c:\java
parsing WSDL...

Generating code...
Compiling code...

Go to the output folder and you will see these files:


To use them, create a new java project:


And a package named "com.l3oc", then copy just the two generated .java files and create a new class named "Main":


With this content:

package com.l3oc;

public class Main {
 
 
 public static void main(String[] args)
 {
          CalculatorImplService calcImpl = new CalculatorImplService();
          Calculator calc = calcImpl.getCalculatorImplPort();
        
          System.out.println("3+1="+calc.sum(3, 1));
          System.out.println("5/2="+calc.divide(5, 2));
          System.out.println("5-4="+calc.subtract(5, 4));
          System.out.println("3*2="+calc.multiply(3, 2));
 }
 
}

Thats all! the video below show the result, first without run the web service (to see the client fail) and then running it:



Download
About the versions
  • Eclipse Java EE IDE for Web Developers Luna Service Release 1 (4.4.1)
  • Tomcat Server 8.0
  • JAX-WS RI and wsimport version 2.2.8

0 comentários :

Post a Comment