![]() ![]() |
Client-Side ProgrammingAccessing a Web service from the client can be (almost) as easy. In the simplest case of wanting to access an RPC SOAP service, let's take a closer look at the example from Chapter 3, shown in Listing 4.3. Listing 4.3 InventoryCheckClient.javapackage ch3.ex2; import org.apache.axis.client.ServiceClient; /* * Inventory check web service client */ public class InventoryCheckClient { /** * Service URL */ String url; /** * Point a client at a given service URL */ public InventoryCheckClient(String url) { this.url = url; } /** * Invoke the inventory check web service */ public boolean doCheck(String sku, int quantity) throws Exception { ServiceClient call = new ServiceClient(url); Object[] params = new Object[]{ sku, new Integer(quantity), } ; Boolean result = (Boolean)call.invoke("", "doCheck", params); return result.booleanValue(); } } As shown here, a ServiceClient Sometimes each parameter passed to the method needs to have a specific name associated with it. For example, some SOAP servers will use the parameter names in the method-matching algorithm. In these cases, a slight change to the way invoke() is called is required: Boolean result = (Boolean) call.invoke( "", "doCheck", new Object[] { new RPCParam("skuName", sku), new RPCParam("quantity", new Integer(quantity))} ); Notice that now instead of passing in an array of Java objects, an array of RPCParams is passed in, where each RPCParam consists of the name of the parameter (skuName and quantity in this example) and the value of the parameter (sku and quantity). When talking with an Axis server or any other SOAP server that does explicit typing of the XML stream (this means the datatype of the parameters and return value of the RPC call is placed in the SOAP message), the Axis client can use that typing information to know how to deserialize the return value. However, some SOAP servers do not do this; in this instance, they are expecting the client to know the datatype through some other means (perhaps WSDL). When this occurs, it becomes the responsibility of the client application to tell the Axis client what type the return value is—which just requires a couple lines of code. The complete client application looks like Listing 4.4 Listing 4.4 InventoryCheckClient.javapackage ch3.ex2; import org.apache.axis.client.ServiceClient; /* * Inventory check web service client */ public class InventoryCheckClient { /** * Service URL */ private String url; /** * Point a client at a given service URL */ public InventoryCheckClient(String targetUrl) { url = targetUrl; } /** * Invoke the inventory check web service */ public boolean doCheck(String sku, int quantity) throws Exception { ServiceClient call = new ServiceClient(url); ServiceDecription sd = new ServiceDescription("return", true); sd.setOutputType(new QName(Constants.URI_2001_SCHEMA_XSD, "boolean")); call.setServiceDescription(sd); Boolean result = (Boolean) call.invoke( "", "doCheck", new Object[] { sku, new Integer(quantity) } ); return result.booleanValue(); } } In this example we've added the definition of a ServiceDescription |
![]() ![]() |