• Post Reply Bookmark Topic Watch Topic
  • New Topic

Client/Server passing objects  RSS feed

 
Angela Margot
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
}
}
-------------------------------------
 
Angela Margot
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay...I managed to create a client that passes an object to the service. The service then sends one back to the client. It's just a matter of getting the call together, and putting an encoder and synchronizer on it...
Don't worry....you guys will hear more from me as my problems grow!
 
Angela Margot
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just FYI, in case anyone else tries to do this:
I used the code I listed in my original post - but I had to modify it a bit. I had to define the LeadResult in JavaBean format (like I did for NewLead - getThis, setThis, etc), and I had to create and define mappings for the LeadResult object I was sending back in Client2. So the code would look like:
// prepare the mapping registry
SOAPMappingRegistry registry = new SOAPMappingRegistry();
// set the mapping for the sent object
QName qname = new QName( "urn:test_encoding", "testleads" );
BeanSerializer serializer = new BeanSerializer();
registry.mapTypes( Constants.NS_URI_SOAP_ENC, qname, NewLeads.class, serializer, serializer );
// set the mapping for the received object
qname = new QName( "urn:rcv_encoding", "rcvleads" );
registry.mapTypes( Constants.NS_URI_SOAP_ENC, qname, RcvLeads.class, serializer, serializer );
Then later in Client2 when I wanted to retrieve elements from the object I did:
if( !response.generatedFault() )
{
Parameter result = response.getReturnValue(); // response was OK
// Take the result and put it into the new object
LeadResult rcvLead = (LeadResult) result.getValue();
idname= rcvLead.getName();
pet = rcvLead.getPet();
String pet_lead = rcvLead.getPetLead();
System.out.println( "Lead Result= " + result.getValue() );
System.out.println("NAME: " + idname);
System.out.println("PET: " + pet);
System.out.println("PET LEAD: " + pet_lead);
I believe this encompasses the changes I had to make to have the client send an object to the server, and have the server respond.
Happy Java!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!