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

Do you know your Java?
Take a Ten-Question-Java-Quiz!

Bookmark and Share




Need help with your Java code? It's secure and confidential.
This is how it works:
Send a detailed description of what you need help with, the more details the better. Also provide a deadline for when it has to be finished. More time means better chance of putting your request into the schedule.

If the request is serious you will shortly receive an email with the price, to which you have to respond if you accept.

Once you have accepted, the work will begin on developing your code by an experienced Java developer. When the code is finished a link to a secure payment will be sent to you.

The source code is then sent to you once the payment is completed.

IMPORTANT! The request needs to be very detailed, else it may be ignored.


Write your detailed request here:

E-mail address: