The Apache SOAP implementation started life as a project at IBM. It was then turned over to the Apache group, which maintains the project today. The following instructions focus on helping you get the Apache SOAP implementation up and running, allowing you to easily bind to SOAP methods using Java. Once you get either of the environments up and running, read our primer on writing SOAP clients to start using services.

A Quick-Start Guide for Installing Apache SOAP (version 2.1)

Step 1. Install Java Virtual Machine
Step 2. Install Apache SOAP and Apache Xerces-J (XML Parser)
    Important Note: It is essential that you run version 1.1.2 or later of Xerces-J.

    To download the complete packages and learn more about Apache SOAP and Xerces-J, visit the Apache SOAP Homepage and the Apache Xerces-J Homepage. Alternatively, you can download the following quickstart package:


    The quickstart package contains:

    1. soap.jar version 2.1 - contains Apache's SOAP client classes
    2. xerces.jar version 1.2 - contains Apache's Xerces-J classes
    3. mail.jar and activation.jar - required by Apache SOAP 2.1
    4. Apache license agreement
Step 3: Configure Environment

    Make sure that all of the jar files (xerces,soap,mail,activation) are found within your CLASSPATH. It is vital that you have only one version of xerces.jar in CLASSPATH, and it placed before any other parser you may have also in the CLASSPATH. UNIX users, be sure that the CLASSPATH environment variable is exported.

    Make sure that the JAVA bin directory is in your PATH environment variable.

You are now ready to use the Apache SOAP API.

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:

    call.setParams (params);

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.

Copyright © 2001 XMethods   Comments and Suggestions are welcome. Terms of Use