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
http://localhost:8080/webservice/ExampleWSService?wsdl

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

package com.javaeenotes;

import javax.xml.ws.WebServiceRef;


public class WebserviceClient {

// This annotation only has effect in a container.
@WebServiceRef(
wsdlLocation =
"http://127.0.0.1:8080/webservice/ExampleWSService?wsdl"
)
private static ExampleWSService service;

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

public void start() {
// This statement is not needed when run in container.
service = new ExampleWSService();
ExampleWS port = service.getExampleWSPort();
System.out.println(port.greet("Peter"));
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.

10 comments:

  1. Thanks.This blog post helped me a lot

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

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

      Delete
  3. Thanks ,

    It help me lot go generate the Web service clint.

    ReplyDelete
  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" javax.xml.ws.WebServiceException: 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

    ReplyDelete
    Replies
    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?

      Delete
    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.

      Delete
  5. This comment has been removed by the author.

    ReplyDelete
  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
    tnx

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

      Delete