How To Call A Web-service Using Javaee?
Solution 1:
Ok, stumbled upon your post the second time, so I'll elaborate my comment given before :).
First I recapitulate your set-up:
- You have a working webservice and an URL pointing to the corresponding WSDL
- You'll try to invoke the WS methods from a different Java EE project on a different machine
General options for invoking a WS:
- Use Dependency Injection to inject the WS reference
- Create your own WS stubs
The first option won't work in your set-up because DI will only work in an container-managed-environment (see my comment). That means that the WS class and the executing class have to be in the same container (e.g. the same server).
So what is left is to generate your WS stubs manually. Therefore you can use the wsimport
tool mentioned in your own answer. There are several different ways to use this tool. Lets have a look in the CLI use:
- navigate in your projekt folder of the WS client used by your IDE :
%IDE_WORKSPACE%/your project/src
- crate a new folder, e.g.
stub
- open a command window in this directory
- execute the follwing command :
wsimport -keep <http://yourwsdl?wsdl>
- After a refresh you should see several created files
Back in your IDE:
Now you're able to use your generated stub-files to connect to the WS by getting a port
from the generated service
-file
publicclassWsClient {
publicstaticvoidmain(String[] args) {
//Create Service'GeneratedFile'Service service = new'GeneratedFile'Service();
//create proxy'GeneratedFile' proxy = service.get'GeneratedFile'Port();
//invoke
System.out.println(proxy.yourMethod(yourParam));
}
}
Last hints:
- For portabilty purpose check the generated files. In their annotations sometimes the WSDL file is linked to a local copy. Just change this back to your WSDL-URL.
AFAIK there is an option in the
wsimport
tool to set this directly in the import routine. - There is a plugin for Eclipse called soapUI which allows you to use the
wsimport
tool in a GUI out of Eclipse. Once set up it should accelerate your work. - I've also found a quick start guide in developing WS clients with eclipse.
Hope this helped, have Fun!
EDIT: Just to clarify:
After you used the wsimport
tool you should have a directory containing files like shown in the image. To make this example clear you'll need to get a Service from the RequestFileService
(this is my WS operation) like RequestFileService service = new RequestFileService();
and after this you'll need a Port on this service like RequestFile proxy = service.getRequestFilePort();
.
After this you can invoke your method calls by using the port proxy.yourMethod(yourParam);
Post a Comment for "How To Call A Web-service Using Javaee?"