Friday, October 8, 2010

Web service client with JAX-WS in Eclipse

In this blogpost, I will use JAX-WS to show how easy it is to create a simple client that makes use of the web service we created in the previous blogpost. The client can be implemented in various ways, like a web application or an EJB. In both mentioned ways, you run the client in a Java container. Therefore, you can take advantage of the resource injection facility provided by the container. This means you can use annotations to automatically inject the web service object where needed. We start out by creating a standard client, which does not run in a container.

Let's get started by creating a standard Java project in Eclipse. When done, start up a command prompt and navigate it to the root directory of the newly created project. Use the wsimport command to generate the classes you need to access the web service. You need the URL of the WSDL for this to work. The following command generates the sources and classes, and place them in the default Eclipse directories.

wsimport -s src
-d bin

Now, create a new class, which acts as the client. An example client:

package com.javaeenotes;


public class WebserviceClient {

// This annotation only has effect in a container.
wsdlLocation =
private static ExampleWSService service;

public static void main(String[] args) {
WebserviceClient wsc = new WebserviceClient();

public void start() {
// This statement is not needed when run in container.
service = new ExampleWSService();
ExampleWS port = service.getExampleWSPort();
System.out.println(port.multiply(3, 4));

The resource injection annotation is not necessary here, because the client does not run in a container. I left it here to show how to use annotation to inject the resource into your class when run in a container.


  1. Thanks.This blog post helped me a lot

  2. Can I create a web form to make client enter data and send it to service to process it in Eclipse?

    1. Ofcourse you can! I recommend Apache Wicket for this.

  3. Thanks ,

    It help me lot go generate the Web service clint.

  4. Hi Gin-Lung Cheng,

    first of all - thx for your tutorial.
    Do you have experience using a local wsdl file (I know that this is dirty but I have to edit it)? I am always getting this error when running the web service client:
    Exception in thread "main" Cannot find 'META-INF/Webservice.wsdl' wsdl. Place the resource correctly in the classpath.
    The web service client is a Maven Java application. The folder 'META-INF/Webservice.wsdl' is the the wsdllocation used by wsimport.
    Do you know a solution for this problem (where to put the wsdl file so that it is found while running the app)? Maven does not put it in the jar. I have to do it manually but it does not help.

    Appreciate you input

    1. Hi Tokad,

      I'm not sure what you're trying to do here, but the WSDL should be served by the webservice. The @WebServiceRef has an URL attribute that points to the WSDL file. Are you developing a service and a client at the same time?

    2. Thanks for your answer. I am not developing a client and a service, but it is possible to use a local wsdl file for the webservice. In my case this is neccesary because I had to edit URLs in the wsdl file. So it is stored locally.

  5. This comment has been removed by the author.

  6. hi , tnx for the tutorial , i want to know how to use wsimport with a protected wsdl by login and password , i m looking for a plugin or comand to do that

    1. Maybe you can save the file locally after logging in. And generate your classes using the local file.