Writing a Client Application using Apache SOAP
Classpath
Your CLASSPATH environment variable should have both the "soap.jar" and "xerces.jar" JAR files included.
Importing packages
For basic SOAP method invocation, you should import the following at minimum:
// Required due to use of URL class , required by Call class
import java.net.*;
// Required due to use of Vector class
import java.util.*;
// Apache SOAP classes used by client
import org.apache.soap.util.xml.*;
import org.apache.soap.*;
import org.apache.soap.rpc.*;
Sample Client Code Walkthrough
The following line sets the default Encoding style to be the standard SOAP encoding
(which is based on XML Schema typing). Other possible types include
NS_URI_LITERAL_XML for encoding DOM objects
call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
The following line sets the Target Object URI, which can be thought of as the object
interface identifier.
call.setTargetObjectURI ("urn:studentProfileSaver");
The following line sets the Method Name that you wish to invoke on the Object Interface
that you described in the line above.
call.setMethodName ("saveProfile");
Now, the parameter vector is setup. The Call object accepts a vector of parameters; these
parameters, each of which represents one argument to the method being invoked, are
serialized "for the wire" using a type mapping system that serializes based on the type
of the object held by each parameter. A different encoding from the "default" encoding
setup above may be associated with an individual parameter. If the type mapping system
detects that the parameter list holds a variable of a type that isn't recognized by the
serializer, an exception will be thrown - for example, if the parameter is labeled with
Literal XML encoding, but you pass in a String object. Literal XML encoding expects a
value of type "Element" and thus does not recognize "String".
Vector params=new Vector();
params.addElement (new Parameter ("StudentID", String.class, studentID, null));
Here we add a parameter that should be encoded with a different serializer (in this case,
the XML serializer) than the default encoding set at the Call object level.
params.addElement(new Parameter ("StudentProfile", Element.class, profile, Constants.NS_URI_LITERAL_XML));
So it's clear now that we are sending two parameters to
the method being invoked - a string, and an XML document.
Note that the XML document could have also been sent as a string,
in the default SOAP encoding.
Now, we add the parameter to the call object:
and then invoke the method.
URL url = new URL ("http://www.mySoapRouter.com/soap/servlet/rpcrouter");
Response resp = call.invoke (url, "");
The URL object represents the SOAP endpoint that will be receiving the
request. Typically (at least in Apache SOAP server implementations) ,
a single SOAP endpoint is capable of handling calls to multiple object
interfaces and methods.
Note that the invocation may throw a SoapException if something goes
wrong, so you will need to make provisions to catch the exception.
Next, you will want to check the response to see if a exception was
generated at the SOAP protocol level (such an exception would not throw
a SoapException fault, which can be thought of as a "lower level" fault).
if (resp.generatedFault()) {
Fault fault=resp.getFault();
System.out.println(" Fault code: " + fault.getFaultCode());
System.out.println(" Fault string: "+fault.getFaultString());
} else {
Otherwise, the call was successful and a return value can be retrieved.
Parameter stores values of class Object, so you will need to downcast
the object to expected type for further handling.
Parameter result=resp.getReturnValue();
Object o = result.getValue();
}
At this point, the method invocation is complete.