Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Clean up your wire protocol with SOAP, Part 2

Use Apache SOAP to create SOAP-based applications

  • Print
  • Feedback

Page 6 of 7

  • The object ID of the service being invoked, which is set via the setTargetObjectURI() method on the Call object. Your object ID is urn:Hello.
  • The name of the method to invoke on the service, which is set via the setMethodName() method on the Call object. Your method name is sayHelloTo.
  • The encoding style used to encode the parameters, which is set via the setEncodingStyleURI() method on the Call object. You are interested in the standard SOAP encoding style defined by the namespace http://schemas.xmlsoap.org/soap/encoding/.
  • The parameters for the method call set via the setParams() method on the Call object. The setParams() method takes a Java Vector as its parameter. The vector contains all the parameters, with index 0 being the first parameter in the method signature starting from the left, index 1 being the second parameter starting from the left, and so on. Each element in the vector is an instance of org.apache.soap.rpc.Parameter. The Parameter constructor takes the parameter's name, Java type, and value, as well as an optional encoding style. If a null encoding style is provided (as is done here), then the Call object's encoding style will be used. Even though each parameter corresponds to a name, that name can be set to anything and is not used by the Apache SOAP server while invoking the method. Therefore, it is absolutely imperative that you maintain the same order of the parameters in the vector and the method signature.


The code snippet below shows how the client creates the Call object:

// Build the call.
Call call = new Call();
call.setTargetObjectURI("urn:Hello");
call.setMethodName("sayHelloTo");
call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);         
Vector params = new Vector();         
params.addElement(new Parameter("name", String.class, name, null));
call.setParams(params);


Now it's time to actually invoke the method on the remote HelloWorld service. To do so, the client calls the invoke() method on the Call object. This method returns an org.apache.soap.rpc.Response object, as shown below:

// Invoke the call.
Response resp = null;         
try
{
   resp = call.invoke(url, "");
}
catch( SOAPException e )
{
   System.err.println("Caught SOAPException (" + e.getFaultCode() + "): " + e.getMessage());
   System.exit(-1);
}
Next, the client checks the Response object. If the method invocation results in an error, the generatedFault() method returns a true value, and the actual fault is retrieved and displayed:

Fault fault = resp.getFault();            
System.err.println("Generated fault: ");
System.out.println ("  Fault Code   = " + fault.getFaultCode());  
System.out.println ("  Fault String = " + fault.getFaultString());
    


If the method invocation succeeds, the returned hello message is retrieved and displayed:

// Check the response.
if( !resp.generatedFault() )
{
   Parameter ret = resp.getReturnValue();
   Object value = ret.getValue();            
   System.out.println(value);
}


The HelloWorld example with JavaBeans

As I mentioned earlier, Apache SOAP provides a number of prebuilt serializers and deserializers. These include serializers and deserializers for using Java Vectors, Enumerations, arrays, and JavaBeans as parameters and return values. In this section, I'll modify the HelloWorld service to use a JavaBean name to pass in the person's name that receives the hello message.

  • Print
  • Feedback

Resources