java2db

The wsimport to create web client from WSDL ( JAX-WS )



The wsimport  command from JDK to create a java WS client .The location of the WSDL  (URI)  is required  to use wsimport to create a web client . Along with the wsimport  command , we can specify some additional options . The list of supported options are given below.

 

 Option  Description
 -d <directory>  Specify the location to place generated output files.
 -b <path> Specify external JAX-WS or JAXB binding files (Each <file> must have its own -b).
 -B <jaxbOption>  Pass this option to JAXB schema compiler.
 -catalog  Specify catalog file to resolve external entity references, it supports TR9401, XCatalog, and OASIS XML Catalog format.
 -extension  Allow vendor extensions (functionality not specified by the specification).
 -help  Display help.
-httpproxy:<host>:<port>  Specify an HTTP proxy server (port defaults to 8080).
-keep  Keep generated files.
-p  Specifying a target package (package name) via this command-line option.
-s <directory>  Specify where to place generated source files.
-verbose  Output messages about what the compiler is doing.
-version  Print version information.
-wsdllocation <location>  @WebServiceClient.wsdlLocation value.
-target  Generate code as per the given JAX-WS specification version.
-quiet  Suppress wsimport output.

A lot of online web services are available . In the below example , we used the WSDL file from www.webservicex.com .

 

 

 Example to use wsimport to create web client :

 Step 1 :

Copy the URL http://quicksoftwaretesting.com/sample-wsdl-urls-testing-soapui/ in any web browser.

There you can find the WSDL file URI  http://www.webservicex.com/globalweather.asnx?wsdl  .

(Copy the WSDL file URI  in any web browser address bar, the WSDL will load on the browser) .

 

 

The wsimport to create web client from WSDL Step 2 :

Add java home (C:\Program Files\Java\jdk1.6.0_24\bin) to the environment variables.If it is already there then ignore this step.

 

 

The wsimport to create web client from WSDL Step 3 :

Open the command prompt and create a  test_WS  (any name)  folder (with md test_WS command ). Open test_WS directory (using cd test_WS  command).

 

 

The wsimport to create web client from WSDL Step 4 :

Now copy the  command wsimport -p testWS -keep  http://www.webservicex.com/globalweather.asnx?wsdl .

-p is for specifying a target package , testWS is the package name.

-keep  option is used to get .java files along with .class files .

Now you can find a folder with the package called testWS  which contains .java and .class files.

 

 

The wsimport to create web client from WSDL Step 5 :

Create a java project in any IDE like  Eclipse , Myeclipse , NetBeans or RAD.In this example , we used Eclipse .

Copy the folder (package ) testWs in the source (src) file of the project.

Now create a java class ( ClientToUseWS.java ) to use the web service in the web client. The complete directory structure as follows.

 

 

 Step 6 :

To use the web service from the service provider , we have to initialize the service class in the ClientToUseWS.java class . To know the service name , open the WSDL in the browser and find it.

From the below piece of  WSDL , GlobalWeather is the service name .

 

 

 Step 7 :

Copy the below code in the ClientToUseWS.java  file . and run it as a stand alone java program .

 

 

 Step 8 :

Finally we will get the below output in the console .




Related Posts :
Create a java web service from WSDL (Top down approach) in Eclipse
What is WSDL , Use of WSDL in java Web Services
   [will not be published]



^ <