Thursday, April 19, 2012

Consuming JAX-WS Web Services in Eclipse

1. Consuming JAX-WS Web Services in Eclipse:


In my previous post I developed a Java Project which exposes a web service. Click here.
This post we will try to consume the web service which we developed.

1. Open Eclipse and create a Java Project.
     Name the project as WebserviceClient. and Click on Finish.
     Note: Do remember to Select JRE as javaSE-1.6.





   2.  Generate the required client files using WSIMPORT tool. This is also part of JDK 1.6 distribution.          
        You can find the wsimport.exe in the %JAVA_HOME%/bin folder.
        Open the command window and navigate to the project home\bin folder. 
        Enter the wsimport command as given below.


       
      Refresh the WebserivcesClient project in the eclipse. a "com.mycompany.service" package will be added to the src folder.


3.  Right Click on the src folder in the WebserviceClient Project and select Class option.
     Enter the class name as "ConverterClient" and package as "com.arun.ws.client".
     Check the main method option.
      
  
            Replace the class content with the below.


    package com.arun.ws.client;

    import com.mycompany.service.Converter;
    import com.mycompany.service.ConverterImplService;
    public class ConverterClient {

          /**
           * @param args
           */
          public static void main(String[] args) {
                ConverterImplService service = new ConverterImplService();
                Converter converter = service.getConverterImplPort();
                System.out.println(converter.farenheitToCelcius(101));
                System.out.println(converter.celciusToFarenheit(38.36));
          }
    }
           Run the class.

           Output:
38.333333333333336


You have successfully comsumed the JAX-WS web services in Eclipse.


101.0048