Web Services

Date: 19/June/2002

How to write a Web service client

Introduction

A Web services client is basically a SOAP client. Given a WSDL (Web Services Definition Language) file, let us see how to generate a stub for the web service and write a client which access the web service through the stub.

Description

    Let us look at how to write a simple Web service client for a Oracle9iAS J2EE Web service. Our web service has two methods, one which returns an exchange rate between two currencies and another which converts currencies. The method definitions that are part of the WSDL for our discussion is:
   ...
    <message name="convert0Request">
      <part name="fromCurrency" type="xsd:string"/>
      <part name="toCurrency" type="xsd:string"/>
      <part name="amount" type="xsd:float"/>
   </message>
   <message name="convert0Response">
      <part name="return" type="xsd:float"/>
   </message>
   <message name="exchangeRate1Request">
      <part name="from" type="xsd:string"/>
      <part name="to" type="xsd:string"/>
   </message>
   <message name="exchangeRate1Response">
      <part name="return" type="xsd:float"/>
   </message>
...
The complete WSDL file can be found here.

Oracle9iAS J2EE Web services infrastructure requires a stub, which will be used by the client to access the web service. The following listing shows the stub which encapsulates the actual method calls.

     public String endpoint =
     "http://localhost:8888/Workspace2-Project1-context-root/CurrencyConvertor";
     private OracleSOAPHTTPConnection m_httpConnection = null;
     // This method encapsulates the invocation details of the convert method.
     public Float convert(String fromCurrency, String toCurrency, Float amount) throws Exception
     {
      Float returnVal = null;
      URL endpointURL = new URL(endpoint);
      Call call = new Call();
      call.setSOAPTransport(m_httpConnection);
      call.setTargetObjectURI("CurrencyConvertor");
      // Preparing to call the method "convert" on the service
      call.setMethodName("convert");
      call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
      // Set up the parameters for the method
      Vector params = new Vector();
      params.addElement(new Parameter("fromCurrency", String.class, fromCurrency, null));
      params.addElement(new Parameter("toCurrency", String.class, toCurrency, null));
      params.addElement(new Parameter("amount", Float.class, amount, null));
      // Associate the created parameters with the call object
      call.setParams(params);
      // Invoke the call using the enpointURL defined above
      Response response = call.invoke(endpointURL, "");
      if (!response.generatedFault())
      {
        Parameter result = response.getReturnValue();
        returnVal = (Float)result.getValue();
      }
      else
      {
        Fault fault = response.getFault();
        throw new SOAPException(fault.getFaultCode(), fault.getFaultString());
      }
      return returnVal;
     }
     // This method encapsulates the invocation details of the exchangeRate method
     public Float exchangeRate(String from, String to) throws Exception
     {
      Float returnVal = null;
      URL endpointURL = new URL(endpoint);
      Call call = new Call();
      call.setSOAPTransport(m_httpConnection);
      call.setTargetObjectURI("CurrencyConvertor");
      // Preparing to call the method "exchangeRate" on the service
      call.setMethodName("exchangeRate");
      call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
      // Set up the parameters for the method
      Vector params = new Vector();
      params.addElement(new Parameter("from", String.class, from, null));
      params.addElement(new Parameter("to", String.class, to, null));
      // Associate the created parameters with the call object
      call.setParams(params);
      // Invoke the call using the enpointURL defined above
      Response response = call.invoke(endpointURL, "");
      if (!response.generatedFault())
      {
        Parameter result = response.getReturnValue();
        returnVal = (Float)result.getValue();
      }
      else
      {
        Fault fault = response.getFault();
        throw new SOAPException(fault.getFaultCode(), fault.getFaultString());
      }
      return returnVal;
     }
    ...
The complete stub source listing can be found here.

Once the stub is written / generated, clients can instantiate the stub to invoke the web service. Clients can be any java based clients. For a client to access the stub, the stub class should be present in the classpath of the client application. Assuming the name of the generated client as CurrencyConverterStub.java, it needs to be used as follows

...
CurrencyConverterStub handle = new CurrencyConverterStub();
handle.convert("USD","JPY",new Float(50));
....
The complete listing of a simple client can be found here.

This snippet shows the steps that needs to be followed to develop a simple web services client and to consume a service. You can find a fully functional sample here.

Note : Oracle9i JDeveloper makes development of Web services simpler. It also facilitates the generation of WSDL files, stubs through a wizard based interface. You can download Oracle9i JDeveloper from here.

Resources



 
E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy