I've recently done that. In the following code the client sends an object (made up of a name and a pet) to a service, then the service returns a "GOOD PET" or "BAD PET"
string based upon the name of the pet.
Does this help?
-----------------------------------------------
package test2;
/**
* This is the Client information - it does the sending to service
*/
import java.net.*;
import java.util.*;
import org.apache.soap.*; // Body, Envelope, Fault, Header
import org.apache.soap.encoding.SOAPMappingRegistry;
import org.apache.soap.encoding.soapenc.BeanSerializer;
import org.apache.soap.rpc.*; // Call, Parameter, Response
import org.apache.soap.util.xml.QName;
public class Client2
{
public static void main( String[] args ) throws Exception
{
String idname = "";
String pet = "";
if (args.length > 0) {
for (int i = 0; i<args.length-1; i++)
{
if (i>0)
idname += " ";
idname += args[i];
}
pet = args[args.length-1];
} else {
System.err.println("Usage:
java test2.Client2 name pet");
return;
}
URL url = new URL( "http://127.0.0.1:8070/soap/servlet/rpcrouter" );
String urn = "urn
rocessmsg2";
// prepare the mapping registry
SOAPMappingRegistry registry = new SOAPMappingRegistry();
// set the urn and the name
QName qname = new QName( "urn:my_encoding2", "newleads" );
BeanSerializer serializer = new BeanSerializer();
// Set up the registry mapping for the encoder, urn, classes, and serializer
registry.mapTypes( Constants.NS_URI_SOAP_ENC, qname, NewLeads.class, serializer, serializer );
// prepare the service invocation
Call call = new Call();
// set the mapping registry for the call
call.setSOAPMappingRegistry( registry );
// Set the URN for the call
call.setTargetObjectURI( urn );
// Set the method used by call
call.setMethodName( "receive" );
// Set the encoding style used by that URI in the call
call.setEncodingStyleURI( Constants.NS_URI_SOAP_ENC );
// Create a vector to hold the parameters
Vector params = new Vector();
// create a vector using a new lead
// This creates a NewLeads object and puts it in as a parameter
params.addElement( new Parameter( "newlead", NewLeads.class, new NewLeads( idname, pet ), null ) );
// Set the parameters in the call
call.setParams( params );
try
{
System.out.println( "invoke service\n" + " URL= " + url + "\n URN= " + urn );
Response response = call.invoke( url, "" ); // invoke the service
if( !response.generatedFault() )
{
// Put the result into a parameter - then print it out
Parameter result = response.getReturnValue(); // response was OK
System.out.println( "Result= " + result.getValue() );
}
else
{
Fault f = response.getFault(); // an error occurred
System.err.println("PROBLEM WITH TRANSMISSION:");
System.err.println( "Fault= " + f.getFaultCode() + ", " + f.getFaultString() );
}
}
catch( SOAPException e ) // call could not be sent properly
{
System.err.println( "NO CONNECTION: " );
System.err.println( "SOAPException= " + e.getFaultCode() + ", " + e.getMessage() );
}
}
}
-----------------------------------------------
package test2;
/** Sets the interface for the function **/
public interface ILeads
{
String receive( NewLeads newleads);
}
------------------------------------------------
package test2;
/** This creates the object that the client sends */
public class NewLeads
{
String idname="";
String pet="";
public NewLeads()
{
}
public NewLeads( String idname, String pet )
{
this.idname = idname;
this.pet = pet;
}
public String toString()
{
return "NewLeads( " + idname + ", " + pet + " )";
}
public void setName( String idname )
{
this.idname = idname;
}
public String getName()
{
return idname;
}
public void setPet( String pet )
{
this.pet = pet;
}
public String getPet()
{
return pet;
}
}
--------------------------------------------------
package test2;
/** This is the class that responds to the clients request **/
public class Leads implements ILeads
{
public String receive( NewLeads newlead )
{
String myanswer = "";
System.out.println( "got new lead " + newlead );
// This is a check to see if the lead is good -- could be changed later to
// another comparison
if (newlead.getName().equals("BOGUS"))
return "BAD PET";
else
return "GOOD PET";
//return newlead;
}
}