mardi 23 septembre 2008

How to use a wsdl file locally from a client code

From a WS client point of view, it would be interesting to retrieve a wsdl file locally instead of calling a remote web server. This method garantee that if the server add some new feature in the WS port you use, you do not have to rebuild your client code.

To do so, you need to generate your stub specifiying a local wsdl file. In this way, the path to access the wsdl file is relative to the path of the package where you generate your stubs.

For example with wsimport you can ask it to generate the stub in the test.example.generated package ( wsimport … -p test.example.generated …) and referes locally your wsdl file (if wsdl files stored in /wsd => wsimport … -p test.example.generated –wsdllocation ../../../wsdl/mywsdlfile.xml).

What is the impact on you client code ?
For example if you try to use a web service called MyHelloWorld and if you generate your stubs in the test.example.generated package, your client code will look like :

test.example.generated.MyHelloWorldService service = new test.example.generated.MyHelloWorldService();
test.example.generated.MyHelloWorld helloPort = service.getMyHelloWorldPort();
BindingProvider provider = (BindingProvider) helloPort;
provider.getRequestContext().put(
BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
"http://localhost:8080/MyHelloWorld/MyHelloWorld"
);


How to package your client
To package you client jar, you need to add :
  • the stubs generated by the wsimport utility
  • the wsdl files and associated xsd,
  • and your client code.
That's all...