7.
Creating a Client for the Web Service
Now that we have deployed our web service and tested it, we will
use MyEclipse Blue to generate a web service client for us. The
web service client will allow us to interact directly with the
web service and all it's exposed operations without needing to
write all the marshalling or connection code ourselves.
In MyEclipse Blue, while you are generating your web service, you
are given the option to generate a client for that web service at
the same time. You are certainly welcome to use that feature any
time you wish, but for the purposes of this tutorial we felt a
more typical situation you might find yourself in, is needing to
create a web service client against a service that has already
been deployed and not one you are generating. Because of this, in
this section we will generate the web service client in a
separate Java Project, as if this web service were already
deployed and we just wanted to hook up to it. This will keep the
lines between web service and web service client creation very
clearly separated for this tutorial, which we hope makes the
topic easier to learn.
Anyway, as we mentioned the first step to create the web service
client will be to create a new Java Project to put the code into.
We will do that by clicking the new toolbar button, then clicking
Java Project:
Now give your project a name and click
Finish:
After the project has been created, we now want to create a new
Web Service Client. Select New Web Service Client from the web service toolbar menu:
In the New Web Service Client wizard select WebSphere 6.1 as the
Target Server. This provides the web service generation services
that MyEclipse Blue will invoke to create your JAX-RPC client.
In this tutorial we will also be selecting the
JAX-RPC framework for our web service client,
but you are certainly free to generate any other kind of client
you wish:
The last step of the web service client creation is to specify
either a
WSDL File or a
WSDL URL for the wizard to retrieve the web
service WSDL from. In our case we are using the URL:
http://localhost:9081/WebServiceProject/services/Calculator?WSDL NOTE: The default WebSphere
port is 9080, if the URL above doesn't work for you, try changing the
port from 9081 to 9080 and try again.
You can use the namespace to package mapping list to customize the packages generated based on the namespaces defined in the source WSDL file.
If you don't specify a mapping, the generation tools will choose an appropriate default.
Now MyEclipse Blue will load the WSDL for the web service you are
attempting to create a client for and validate it for you,
letting you know of any problems that may exist with the WSDL:
NOTE:
If any error occurs with validation, make sure the web
service is deployed and the application server hosting it is
running. If you are trying to generate a client to a 3rd party
web service and get errors during the validation process, please
bring it to the attention of the author of the web service if
possible so it can be corrected.
Once the validation process is done, click
Next to review the libraries that will be added
to your project's build path
:
Now you can click
Finish to have the client generated for you:
After the client has been generated for you, you'll notice a new
package in your Source folder as well as a few new classes that
you can use to work with your web service:
With the new generated resources we can utilize the
CalculatorServiceLocator class to access a
reference to our web service and then execute the operations that
we exposed for it (
add, subtract, multiply and
divide).
In this tutorial let's write some code that uses the different
operations from our web service. As an example let's say we want
to compute the following 4 things:
-
Add 3 and 7
-
Subtract 2 from 12
-
Multiply 9 by 9
-
Divide 40 by 2
First we will want to create a new class, with a
main method in it. Just as we did before, we
create a new class, give it the name
WebServiceClient and tell MyEclipse Blue to
generate a
main method for us:
After our class is generated for us, we need to provide an
implementation of the
main method so that it does the 4 mathematical
calculations we listed above. The code, for the entire
main method, to do those calculations with our
web service and then print the results to the console is as
follows:
public static void main(String[] args) throws
ServiceException,
RemoteException {
/* Create a locator instance */
CalculatorServiceLocator locator = new
CalculatorServiceLocator();
/* Get access to the service using the
locator */
Calculator_SEI calculator =
locator.getCalculator();
/* Using the web service, perform the 4
calculations */
System.out.println("1. 3+7=" +
calculator.add(3, 7));
System.out.println("2. 12-2=" +
calculator.subtract(12, 2));
System.out.println("3. 9*9=" +
calculator.multiply(9, 9));
System.out.println("4. 40/2=" +
calculator.divide(40, 2));
}
|
|
Web service client main method
code
|
NOTE:
The reason for defining the main method
to throw the ServiceException and RemoteException is to avoid
having try-catch blocks in the main method
implementation. In the case of writing a real client, catching
and handling the exceptions can be an important part of writing
good code.
Now that our
main method is written, we can run it.
Before we run this new class we just wrote, we need to adjust the
JRE used to build and run this new
Java Project to match the same runtime used for
the application server we are deploying to (WebSphere 6.1 in this
case).
To do this, right-click on the
WebServiceClientProject project and go down to
Properties:
Then click on the
Java Build Path preference node, click the
Libraries tab, and then select the
"myeclipse" JRE and click
Remove:
Then click
Add Library..., select
JRE System Library and click
Next:
And then select
Alternative JRE then click the dropdown and be
sure to select the JDK configured to run your application server
(WebSphere 6.1 in this case) then click
Finish:
After you have completed those steps and the web service client
project is setup to run with the same JRE (or JDK) used to launch
your application server, you can finally run this class by
right-clicking on it, going down to
Run As (or
Debug As) and selecting
Java Application:
When we do that, our client code runs, accesses our web service
using the locator class generated for it and then we get the
following output in our console:
1. 3+7=10
2. 12-2=10
3. 9*9=81
4. 40/2=20
|
|
Console output from the web
service client
|
And that's all there is to generating and using web service
clients for JAX-WS, JAX-RPC or XFire web services using MyEclipse
Blue.
|