In my previous post, I discussed the creation
of a web service. That’s all good and dandy, but can be rather boring because we
can’t do anything with it other than touch it with a web browser and see a bunch
of XML. Although this example is very specific to the web service created
prior, the concepts can be applied to most web services.
Generating Classes from a WSDL
This project is different than some because it starts
with a build.xml. Within it is the necessary task to generate *.java files we
will use later for communicating with the web service.
There are two key parts to this build file, the taskdef that declares what the wsimport
tag actually is and the task generate-client that uses wsimport to
generate the java that provides an interface to the webservice. If you look
within your src directory, you will now notice that there are now
*.java files for use with your own code. Let’s get to using it.
Writing the Client
The code is relatively straightforward. We make an import call to
the generated classes. Next we create a new MathExampleImpl object
that is our interface to the webservice. Finally, we run a few test methods
on it and return the result to the console so we can see the result.
Build the Client
Adding the above into your build file within the project element will allow
you to build the client. Essentially it’s just a javac call with some
arguments. Having it in the build file allows you to not have to type all
that stuff into the command line every time you compile - much more
convenient. Run the compiled client and you should now see the results you
printed to the console. Next time, doing the same thing via .NET in C