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

Reacções:

1 comentários:

I would like to confirm if you received my previous request regarding the use of the image available on your website:

http://informatictips.blogspot.com.br/

As informed earlier, I work at the iconography department of Yan Imagens, in Sao Paulo, Brazil, and we're producing for Moderna Publisher a didactic material/book called "Textual genres: animated videos" in which the editors would like to use the image above.

If you need any additional information, we’ll be pleased to inform you.