Wednesday, May 14, 2008

Dynamic web service call in Java

How do I call a web service programatically, knowing only the URL of WSDL describing the service. I have thought of two options, each with it's pro's and con's.

1. Executing wsimport. This means executing this command from code, passing parameters such as wsdl url or folder where to import. After this, using a ClassLoader, load the serivice class, wich always has service.getQName().getLocalPart(), where service is a javax.wsdl.Service. This means parsing the wsdl, with a tool such as wsdl4j. Then, find the methods that have @WebEndpoint addnotation, using Reflection API ... then load that class, then find the parameters needed and finally invoke. Sounds complicated? It is, but comparing to

2. Use Dispatch API. This means creating the SOAP message by hand. And this is the hard part. What if a web service method requires complex types? Parse the wsdl and find them. But they are not always in the wsdl, they can be in an attached xsd schema. Hmm ..... And what if the webservice is using WS-Policy, WS-Addresing or some other WS-* standard? Parse the wsdl to find how to construct the SOAP message. And .... configure headers.

I think using wsimport is a much simple way, even if it may take more time to complete the task. You can easily configure security or addresing. And even if it sound complicated, it is much simpler to implement.

No comments: