1. Web service example: order processing
The reason why I choose "Order Processing" is an example because it is close to the actual commercial use case. The web service is capable of updating a given order. In order to achieve this, it must have two methods: ProcessOrder and UpdateOrder. Method ProcessOrder has an in-parameter ORDERID and an Order object as an OUT parameter. The ProcessOrder method returns a status string. Method UpdateOrder uses an ORDER object as the inout parameter, which updates ORDERDATE and returns the Order object to the client. Since these two methods have been used for a complex data type ORDER, it is more precisely that it is used as an OUT / INOUT parameter, so it is necessary to develop a Holder class. The following list 1 and Listing 2 give the ORDER class and the corresponding HOLDER class, respectively (for explanation, all code is placed in the SAMPLE package):
Listing 1: ORDER class
Package Sample;
Public class order {
// id for order
Private string ORDERID = NULL;
// Date of Order
Private string orderdate = null;
// getter methodspublic string getorderId () {
Return OrderId;
}
Public string getorderId () {
Return OrderId;
}
// setter methods
Public void setorderId (string orderid) {
THIS.Orderid = OrderId;
} public void setorderdate (string orderdate) {
THIS. OrderDate = OrderDate;
}
}
Listing 2: ORDER class corresponding Holder class
// Note That Holder Class Is in The Holders Package and ITS Name
// is Derived by adding Holder as a suffix to 'Order', as per
// The JAX-RPC Specification.
Package Sample.holders;
Public class orderholder {
// Order's Object
Public order value = null;
// Default Constructionor
Public void OrderHolder () {}
// Constructor, Which Takes Value As a parameter
Public void ORDERHOLDER (ORDER VALUE) {
THIS.VALUE = VALUE;
}
}
Now let's develop web services with the above features. Listing 3 gives the corresponding code.
Listing 3: Order Processing Web Services
Package Sample;
Public class orderprocessingservice {
//Method 1: Processes a Order Given ID AS INPUT AND
// Return Status and ORDER Object As an Out Parameter
Public String ProcessOrder (String ORDERID,
ORDERHOLDER ORDERHOLDER) {string status = "pending";
// Perform Business Logic Here
// for simplicity just flilling the order object jject
Order Order = New Order ();
Order.setOrderId (OrderID);
Order.setorderdate ("
03 March 2003
");
// set the Holder Value to the order.
ORDERHOLDER.VALUE = Order;
// set the status
Status = "Complete";
Return status;
}
//Method 2: Updates a Order Given Order as an inout
// Parameter and return status.
Public String UpdateOrder (ORDERHOLDER ORDERHOLDER) {
String status = "pending";
// Perform Update Here
ORDER ORDER = OrderHolder.Value;
Order.setorderdate ("
03 April 2003
");
// Note That Orderid Is Not Changd.
// it will be same as the passed one.
// set the Holder Value to the order.
ORDERHOLDER.VALUE = Order;
// set the status
Status = "Complete";
Return status;
}
}
At this point, we have completed the development of Web services. The next step is to compile and deploy it to the Tomcat-Axis platform. After compiling, we need to deploy the above Web services to Tomcat-Axis with the deployment description file.
Listing 4: Deployment file deploy.wsdd
XMLns: java = "http://xml.apache.org/axis/wsdd/providers/java"> Value = "Sample.OrderprocessingService" /> Operation> service> deployment> The above deployment description file is actually aware of some information about the web service, such as disclosed methods, desired parameters, and returning value. Deploy ORDERPROCESSINGSERVICE, we need to pass parameters "deploy.wsdd", call the Axis Admin service. The Admin service running on the same server will process the description file, deploy the web service, to this, it can be called by the client. Run the following command in the same directory as the deploy.wsdd file: Java -cp% AxisclassPath% org.apache.axis.client.adminClient -lhttp: // localhost: 8080 / axis / service / adminservice deploy.wsdd Among them, AxisClassPath is used to set an AXIS environment (see the Axis Installation Guide for details). You can access the OrderProcessing service by the following address: http: // For example, the address is as follows: Http: // localhost: 8080 / Axis / Services / OrderProCESSING 2. Web Services ORDERPROCESSING client Dynamic client Dynamic clients are similar to the method of finding the Java class with a reflection API (Reflection APIS). Here, all information, such as target endpoints, method parameters, etc. must be clearly set. The code listed in Listing 5 shows how to write a dynamic client that calls Web Services OrderProcessing's UpdateOrder method. Listing 5: Dynamic client Package sample.cl; 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.setRType (XMLTYPE.XSD_STRING); Order Order = New Order (); Order.setOrderid ("ORDER001"); Order.setorderdate (" 03 March 2003 "); // set end point address Call.SetTargetendPointDress ( "http: // localhost: 8080 / axis / service / 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"); } 3. Run the client Run the client with the following command: The results are as follows: Get output parameters (as mentioned when developing clients). 4 Conclusion This article tried to uncover the mystery of the web service, express how simple and affordable JAX-RPC-based web services using Apache open source AXIS tools. This article also explains in a way "how to develop" JAX-RPC-based web services. This development method gives developers fully freely write web services and clients, hidden all complex details of the ON-the-Wire XML format sequence object. For developers, it seems to be just a Java method call. Next part of this article, I will explain other methods and techniques of web services. 5. Reference l http://jakarta.apache.org/tomcat/index.html l http://ws.apache.org/axis/ l http://java.sun.com/xml/jaxrpc