Create a Web Service Client with JAX-WS

This example shows how to create a web service client using JAX-WS reference implementation and its tool 'wsimport'. JAX-WS RI can be downloaded from Sun (http://java.sun.com). To create the web service client we need an existing web service, so we will use the one created in the example 'Create a simple Web Service': Click here to go to the 'Create a simple Web Service' example So we assume the web service is deployed on our local computer and it listens to port 8080. Go to the bin directory in the jax-ws ri installation directory and run the command wsimport.bat (if Windows, else wsimport.sh) and pass the url to the wsdl-file as a parameter. wsimport http://localhost:8080/JavaDbExamplesWeb/JavadbWebServiceService?WSDL There are a number of options you can pass to the wsimport utility so you might want to run wsimport -help to find out more. For example, you might want to use the option -keep to prevent the wsimport utility to delete the .java files after compilation. Now the utility has read the wsdl-file, created the necessary classes and compiled them for us to use. We can now create the class that will use them and call the web service.


package com.javadb.examples;

import com.javadb.ws.example.JavadbWebService;
import com.javadb.ws.example.JavadbWebServiceService;

/**
 *
 * @author www.javadb.com 
 */
public class Main {

    public void callWebService() {

        /* Note, the JavadbWebServiceService class has two constructors.
         * The default one that we use here uses the wsdl-location that was
         * specified at the time of the generation of the client classes, 
         * i.e. localhost. If the service is deployed at 
         * another location you need to use the constructor:
         * public JavadbWebServiceService(URL wsdlLocation, QName serviceName)
         */
        JavadbWebServiceService service = new JavadbWebServiceService();
        JavadbWebService port = service.getJavadbWebServicePort(); 
        
        String currentTime = port.getTime();
        
        System.out.println("Current time is: " + currentTime);
        
        
    }

    public static void main(String[] args) {
        new Main().callWebService();
    }
}
Note that you need the genereated classes and the jax-ws libraries in the classpath when you compile and run the above class. That is easiest done by pointing them out in your project properties in the IDE. The output looks something like this: Current time is: 15:54

1 comentários:

Anónimo disse...

Top 5 best online casino - Kadangpintar
The best online casino for players in 메리트 카지노 고객센터 Malaysia. Play kadangpintar exciting online slots, table games, live poker, slots 제왕카지노 and more. Get your bonus here!

Enviar um comentário