Consuming a Web Service using JAX-WS Reference Implementation


This example uses JAX-WS 2.0 API and is available in core Java in JDK 1.5.0.

  1. In the bin directory is a utility called wsimport. This tool has several options, we will use the following:
    • -p: The name of the package where the source files will be generated.
    • -s: The directory path where the source files will be kept.
    • -d: The directory path where the binary files will be kept.
    • The location of the WSDL.
  2. The command prompt line should look like this (all on one line):
    wsimport –p  -s c:/SWS/source –d c:/SWS/bin http://www.centershift.com/STORE40/ 
  3. This generates the proxy class and the following additional classes (use of these classes is described below):
    • ObjectFactory
    • BasicWebServices
    • BasicWebServicesSoap
    • AddMessage
    • AddMessageResponse

Using the Store Web Service methods as Objects


The following is an example of using a sample method:


         //Create the main Web Services object
        BasicWebServices service = new BasicWebServices();
        
        //Create the soap object
        BasicWebServicesSoap soap = service.getBasicWebServicesSoap();

        //Create the factory object
        ObjectFactory factory = new ObjectFactory();

        //Create and set the authentication credentials
        LookupUser_Request lookupUser = factory.createLookupUser_Request();
        lookupUser.setUsername("myUsername");
        lookupUser.setPassword("myPassword");
        lookupUser.setChannel(1);

        //Create and set the authentication credentials
        GetRentalLedger_Request request = factory.create GetRentalLedger_Request ();
        request.setAccountID("123456");

        //Create the method's request object
        GetRentalLedger_Response response = soap.GetRentalLedger(lookupUser, request);

        try{
            //Retrieve the results
            response.getGetRentalLedgerResult();
        }catch{
            //Handle error
        }

    

The first step is to create the three Service objects you will need to consume the services.


        //Create the main Web Services object
        BasicWebServices service = new BasicWebServices();

        //Create the soap object
        BasicWebServicesSoap soap = service.getBasicWebServicesSoap();

        //Create the factory object
        ObjectFactory factory = new ObjectFactory();
    

The basic concept is an object orientated approach of creating a request object, setting the parameters of this request, calling the method and loading the results into a response object. The response will usually be a collection of data that can be bound to display objects.

Each method call requires authentication and takes as parameter a login object which contains a username and password. You can create this object once and pass this into all methods you call.


        //Create and set the authentication credentials
        LookupUser_Request lookupUser = factory.createLookupUser_Request();
        lookupUser.setUsername("myUsername");
        lookupUser.setPassword("myPassword");
        lookupUser.setChannel(1);
    

You then create a request object and set any required parameters. The required parameters are specified for each method in this document. The method will also give you an error message if all required parameters are not set.


        //Create and set the authentication credentials
        GetRentalLedger_Request request = factory.create GetRentalLedger_Request ();
        request.setAccountID("123456");
    

Now create the response object and call the desired method passing in this login object and the request object.


        //Create the method's request object
        GetRentalLedger_Response response = soap.GetRentalLedger(lookupUser, request);

        try{
            //Retrieve the results
            response.getGetRentalLedger_Response();
        }catch{
            //Handle error
        }
    

Always use error handling to catch any problems with your method call. Each SWS method will throw specific error messages to help you debug problems in your code.

This is the basic methodology of all SWS methods.