Although I take showers every day, I'm very new to using
SOAP , and have created a client/server type of app in which the client passes the server an object, and the server returns the same object.
What I would like to do is have the Client app pass the service an object, then the service pass the client another object. I initially thought I could just do a return and the object would be passed, but it has a problem with serializing the object. I know I used the BeanSerializer for the object sent from the Client, but I am not sure where/how to implement it from the service. Is it even possible?
I'd really appreciate any help with this. This is only my second week of coding using SOAP. I'm using the Apache SOAP on a Weblogic server.
Thanks!!
---------------------------------
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 samples.test2.Client2 name pet");
return;
}
URL url = new URL( "http://127.0.0.1:8070/soap/servlet/rpcrouter" );
String urn = "urn:msgsender";
// prepare the mapping registry
SOAPMappingRegistry registry = new SOAPMappingRegistry();
// set the urn and the name
QName qname = new QName( "urn:test_encoding", "testleads" );
BeanSerializer serializer = new BeanSerializer();
registry.mapTypes( Constants.NS_URI_SOAP_ENC, qname, NewLeads.class, serializer, serializer );
// prepare the service invocation
Call call = new Call();
call.setSOAPMappingRegistry( registry );
call.setTargetObjectURI( urn );
call.setMethodName( "receive" );
call.setEncodingStyleURI( Constants.NS_URI_SOAP_ENC );
Vector params = new Vector();
// create a vector using a new lead
params.addElement( new Parameter( "newlead", NewLeads.class, new NewLeads( idname, pet ), null ) );
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() )
{
Parameter result = response.getReturnValue(); // response was OK
System.out.println( "Result= " + result.getValue() );
}
else
{
Fault f = response.getFault(); // an error occurred
System.err.println( "Fault= " + f.getFaultCode() + ", " + f.getFaultString() );
}
}
catch( SOAPException e ) // call could not be sent properly
{
System.err.println( "SOAPException= " + e.getFaultCode() + ", " + e.getMessage() );
}
}
}
---------------------------
// The interface
public interface ILeads
{
LeadResult receive( NewLeads newleads);
}
-------------------------------
// The LeadResult class, should create a new LeadResult object that is sent back to the Client
// I don't think there is anything wrong with this bit of coding that would require serialization.
public class LeadResult {
String idname="";
String pet="";
String pet_lead="";
public LeadResult()
{
}
public LeadResult( String idname, String pet, String pet_lead)
{
this.idname = idname;
this.pet = pet;
this.pet_lead = pet_lead;
}
}
----------------------------------------
// The Leads class - takes in a NewLeads object, returns a LeadResult -- could this be where I'm having problems???
public class Leads implements ILeads
{
public LeadResult receive( NewLeads newlead )
{
String petlead = "";
String idname = "";
String pet = "";
System.out.println( "What kind of lead for - " + newlead );
idname = newlead.getName();
pet = newlead.getPet();
if (idname.equals("Joe"))
petlead = "Collar";
else
petlead = "Harness";
LeadResult myPets= new LeadResult( idname, pet, petlead);
return myPets ;
}
}
------------------------------------
// The class NewLeads
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;
}
}
-------------------------------------