Consume JAX-WS WebService

To consume a JAX-WS webservice you first have to generate portable artifacts to handle the service. Therefor first get the wsdl-file of the service to consume. You should be able to get this by simply adding ?wsdl to the serivce url. Save the wsdl locally and open a terminal. Then you generate the JAX-WS portable artifacts by calling

wsimport -s target/path -p some.target.package SoapService.wsdl

It is also possible to use the http-url in this command. This would look like this for example – mind the ‘ for safety reason:

wsimport -s target/path -p some.package 'http://localhost:8080/SoapService?wsdl'

This should be sufficient to generate all needed artifacts to consume the service. The -s parameter stands for the target directory for the created java files and -p for the package the new artifacts should be contained in. The given target path has to exist before executing the line. The package structure is generated if needed.

If you have created the artifacts as described above you are able to access the service simply by instantiating the class with the name of your service and call the getter for the port that is contained in this class. For example if the webservice has a service interface as the one I used in my example:

SoapService service = new SoapService();
SoapServiceIFace ssif = service.getSoapServicePort();

If you use this in a web application: As far as I know @WebServiceRef should be used to inject an instance to your bean, but in JBoss AS 7.0.2 this seems to be broken at the moment. I’ll update this post as soon as I find a way to do this or the bug is fixed.

Leave a Comment