Developing JAX-RPC-Based Web Services Using Axis and SOAP
OrderProcessing Web Service Client
Dynamic client
Dynamic client is analogous to looking up and invoking the Java class methods using the reflection APIs.
Here, all the information, such as target endpoint, method parameters, and so forth has to be set explicitly. The code shown in Listing 5 will tell how to write a dynamic client for invoking the updateOrder method, in the OrderProcessing Web service.
Listing 5: Dynamic Client
package sample.client; import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.apache.axis.encoding.XMLType; import javax.xml.rpc.ParameterMode; import javax.xml.rpc.encoding.*; import javax.xml.namespace.QName; import java.util.*; import sample.*; /** * This class illustrates how to use the JAX-RPC API to invoke * the Order Processing Web service dynamically */ public class DynamicClient { public static void main(String[] args) throws Exception { // create service factory ServiceFactory factory = ServiceFactory.newInstance(); // define qnames String targetNamespace = "OrderProcessingService"; QName serviceName = new QName(targetNamespace, "OrderProcessingService"); QName portName = new QName(targetNamespace, "OrderProcessingService"); QName operationName = new QName(targetNamespace, "updateOrder"); // create service Service service = new Service(); Call call = (Call) service.createCall(); Qname qn = new Qname(targetNamespace, "OrderHolder"); call.registerTypeMapping(OrderHolder.class, qn, new org.apache.axis.encoding.ser.BeanSerializerFactory (OrderHolder.class, qn), new org.apache.axis.encoding.ser.BeanDeserializerFactory (TicketHolder.class, qn)); // set port and operation name call.setPortTypeName(portName); call.setOperationName(operationName); // add parameters call.addParameter( "arg1", serviceName, ParameterMode.INOUT ); call.setReturnType( XMLType.XSD_STRING ); Order order = new Order (); order.setOrderID("Order001"); order.setOrderDate("03 March 2003"); // set end point address call.setTargetEndpointAddress( "http://localhost:8080/axis/services/OrderProcessing"); // Invoke the WebService String result = (String) call.invoke( new Object[] { order } ); System.out.println("result : " +result); Map outparams = call.getOutputParams(); System.out.println("Got the outparams"); }
Running the Client
To run the client, use this command:
<Prompt>java -cp %AXISCLASSPATH% sample.client.DynamicClient
The result will be:
Got the outparams (as mentioned in client we developed)
Conclusion
This article tried to demystify Web services and tell how easy and economic it is to develop jax-rpc based Web services using an open source tool—Axis from Apache. This article elaborated on "How to Develop" JAX-RPC based Web services in a way that gives the developer a freedom of writing a client and Web service that hides all the complexities of serializing objects in an on-the-wire XML format and, for developers, it will simply appear to be just a Java method invocation. As a next step to this article, I will deal with other methodologies and technologies involved in Web services.
About the Author
Mr. Rajesh Sumra has been working as a Senior Software Engineer for Hewlett-Packard. He has around three years of industry experience. He has extensively worked on Web service technologies such as WSDL, SOAP, and UDDI. Currently, he is involved in designing and developing a Web services-based framework for a mobile infrastructure for HP Labs, Japan. He holds a Masters degree in Information Technology from the Indian Institute of Information Technology, Bangalore. He can be reached at rajeshsumra@yahoo.com.
References
- http://jakarta.apache.org/tomcat/index.html
- http://ws.apache.org/axis/
- http://java.sun.com/xml/jaxrpc
Page 3 of 3
This article was originally published on July 18, 2003