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.
Thanks.This blog post helped me a lot
ReplyDeleteCan I create a web form to make client enter data and send it to service to process it in Eclipse?
ReplyDeleteOfcourse you can! I recommend Apache Wicket for this.
DeleteThanks ,
ReplyDeleteIt help me lot go generate the Web service clint.
Hi Gin-Lung Cheng,
ReplyDeletefirst 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
Hi Tokad,
DeleteI'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?
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.
DeleteThis comment has been removed by the author.
ReplyDeletehi , 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
ReplyDeletetnx
Maybe you can save the file locally after logging in. And generate your classes using the local file.
Delete