This example shows you how to use SAAJ API to invoke a Web service, passing values for input parameters, and getting back the result.
The client implementation uses SAAJ to construct a SOAP Message along with values for the input parameters to invoke the AutoLoan Web service. The source for the client is shown below:
package saaj_autoloan; import java.util.Iterator; import javax.xml.soap.*; import java.net.URL; /** A SAAJ client that invokes the autoloan web service. */ public class Client { public static void main(String[] args) throws Exception { | // get the URL endpoint where the 'autoloan' web service is deployed | URL service = new URL(args.length > 0 ? args[0] : | "http://upload.eraserver.net/circle24/autoloan.asmx"); | | // set the MessageFactory System property | System.setProperty("javax.xml.soap.MessageFactory", | "com.sssw.jbroker.saaj.soap.MessageFactoryImpl"); | | // set the ConnectionFactory System property | System.setProperty("javax.xml.soap.SOAPConnectionFactory", | "com.sssw.jbroker.saaj.soap.SOAPConnectionFactoryImpl"); | | // construct the SOAP message and specify the soapAction | MessageFactory factory = (MessageFactory) MessageFactory.newInstance(); | SOAPMessage message = factory.createMessage(); | message.getSOAPPart().addMimeHeader("soapAction", "http://circle24.com/webservices/Calculate"); | | // get the SOAP envelope and add namespaces | SOAPEnvelope env = message.getSOAPPart().getEnvelope(); | env.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema"); | env.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance"); | | // remove the SOAP Header | // Do this because of a bug in the server at upload.eraserver.net | SOAPHeader hdr = env.getHeader(); | hdr.detachNode(); | | // construct the SOAP body with the Calculate (method) element | SOAPBody body = env.getBody(); | Name name = env.createName("Calculate", "ns1", "http://circle24.com/webservices/"); | SOAPElement child = body.addBodyElement(name); | child.addNamespaceDeclaration("ns1", "http://circle24.com/webservices/"); | child.addAttribute(env.createName("xmlns"), "http://circle24.com/webservices/"); | | // add parameter elements | SOAPElement param = child.addChildElement(env.createName("Months")); | param.addTextNode("24.0"); | param = child.addChildElement(env.createName("RateOfInterest")); | param.addTextNode("8.0"); | param = child.addChildElement(env.createName("Amount")); | param.addTextNode("15000.0"); | | // prepare the message to be sent | message.saveChanges(); | | // send the SOAP message to the specified URL | SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance(); | SOAPConnection connection = scf.createConnection(); | SOAPMessage reply = connection.call(message, service); | connection.close(); | | // extract and print the response | env = reply.getSOAPPart().getEnvelope(); | body = env.getBody(); | Iterator iter = body.getChildElements(); | SOAPElement response = (SOAPElement) iter.next(); | iter = response.getChildElements(); | // There may be some Text elements - so skip them if necessary | Object obj = iter.next(); | while (!(obj instanceof SOAPElement)) obj = iter.next(); | SOAPElement result = (SOAPElement) obj; | System.out.println(result.getValue()); } }
To compile the client:
javac -d WEB-INF/classes src/Client.java
Below is the command line to run the autoloan client:
java saaj_autoloan.Client
The client program will print:
Equated Monthly Instalment (EMI) For the Amount $15000 is $678
Please refer to the README file for details on how to build and run the example.
Copyright © 2003, 2004 Novell, Inc. All rights reserved. Copyright © 2001, 2002, 2003 SilverStream Software, LLC. All rights reserved.