You are viewing a plain text version of this content. The canonical link for it is here.
Posted to soap-dev@xml.apache.org by raja <ra...@innova.stph.net> on 2001/06/21 19:02:54 UTC
Sending paramters
Hi,
I have written a sample application in soap that sends a string to the
server ( here the service object ) and gets an object in return. I am unable
to extract the values from the object ( response object ). Also, i would
like to know how to pass an object ( here a bean ) to the service object. I
have mapped the bean and used a serializer and deserializer object --
BeanSerializer for it. I need to pass values in this object.
here is the code for the client, server and the bean object. Please suggest
The client
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------
import java.io.*;
import java.util.*;
import java.net.*;
import org.w3c.dom.*;
import org.apache.soap.util.xml.*;
import org.apache.soap.*;
import org.apache.soap.encoding.*;
import org.apache.soap.encoding.soapenc.*;
import org.apache.soap.rpc.*;
import samplebean;
class ClientSample
{
public static void main(String arg[]) throws Exception
{
int i = 0;
try
{
SOAPMappingRegistry smr = new SOAPMappingRegistry();
//BeanSerializer beanSer = new BeanSerializer();
BeanSerializer bs = new BeanSerializer();
smr.mapTypes(Constants.NS_URI_SOAP_ENC,
new
QName("urn:sampleservermap", "samplebean1"),
samplebean.class, bs, bs);
Call call = new Call();
call.setTargetObjectURI ("urn:sampleserverret");
call.setMethodName ("samplemethod");
call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
call.setSOAPMappingRegistry(smr);
String symbol = new String("hello");
Vector params = new Vector ();
params.addElement (new Parameter("symbol",
String.class, symbol, null));
call.setParams(params);
URL url = new URL
("http://localhost:8080/soap/servlet/rpcrouter");
Response resp = call.invoke (/* router URL */ url,
/* actionURI */ "" );
// Check the response.
if (resp.generatedFault ()) {
Fault fault = resp.getFault ();
System.out.println ("Ouch, the call failed:
"+fault);
System.out.println (" Fault Code = " +
fault.getFaultCode ());
System.out.println (" Fault String = " +
fault.getFaultString ());
}
else
{
Parameter result = resp.getReturnValue ();
Object value = result.getValue();
samplebean smpbn = (samplebean)value;
System.out.println ("Result is " + smpbn.name +
value);
}
}
catch(Exception e)
{
System.out.println("Exception " +e.getMessage());
}
}
}
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------
The server (service object ) code
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------
import java.util.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import org.apache.soap.util.xml.*;
import samplebean;
public class SampleServer
{
public SampleServer(){
}
public samplebean samplemethod(String str)
{
samplebean sb = new samplebean();
try
{
System.out.println( "Received from client " +str );
sb.name = "raja";
}
catch ( Exception e)
{
System.out.println ("Server " +e.getMessage());
}
finally
{
return sb ;
}
}
}
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------
The bean class
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------
public class samplebean
{
public samplebean()
{
}
public String name = new String();
public int age;
public String getname()
{
return name;
}
}
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------
Please suggest. Thanx in advance.
Raja
Innova Solutions
Ph: Off : 3543139,40,50 X 217
Res : 3551023
RE: Sending paramters
Posted by "Matthew J. Duftler" <du...@watson.ibm.com>.
YOur samplebean class is not really a bean. In short, you should have
methods like:
String getName();
void setName(String name);
int getAge();
void setAge(int age);
You should take a look at the JavaBeans spec on Sun's web-site.
Thanks,
-Matt
> -----Original Message-----
> From: raja [mailto:raja@innova.stph.net]
> Sent: Thursday, June 21, 2001 1:03 PM
> To: soap-user@xml.apache.org; soap-dev@xml.apache.org
> Subject: Sending paramters
>
>
> Hi,
> I have written a sample application in soap that sends a string to the
> server ( here the service object ) and gets an object in return.
> I am unable
> to extract the values from the object ( response object ). Also, i would
> like to know how to pass an object ( here a bean ) to the service
> object. I
> have mapped the bean and used a serializer and deserializer object --
> BeanSerializer for it. I need to pass values in this object.
>
> here is the code for the client, server and the bean object.
> Please suggest
>
> The client
> ------------------------------------------------------------------
> ----------
> ------------------------------------------------------------------
> ----------
> ------------------------------------------------------------------
> ----------
> ----------------------
> import java.io.*;
> import java.util.*;
> import java.net.*;
> import org.w3c.dom.*;
> import org.apache.soap.util.xml.*;
> import org.apache.soap.*;
> import org.apache.soap.encoding.*;
> import org.apache.soap.encoding.soapenc.*;
> import org.apache.soap.rpc.*;
> import samplebean;
> class ClientSample
> {
> public static void main(String arg[]) throws Exception
> {
> int i = 0;
> try
> {
> SOAPMappingRegistry smr = new SOAPMappingRegistry();
> //BeanSerializer beanSer = new BeanSerializer();
> BeanSerializer bs = new BeanSerializer();
> smr.mapTypes(Constants.NS_URI_SOAP_ENC,
> new
> QName("urn:sampleservermap", "samplebean1"),
>
> samplebean.class, bs, bs);
> Call call = new Call();
> call.setTargetObjectURI ("urn:sampleserverret");
> call.setMethodName ("samplemethod");
> call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
> call.setSOAPMappingRegistry(smr);
> String symbol = new String("hello");
> Vector params = new Vector ();
>
> params.addElement (new Parameter("symbol",
> String.class, symbol, null));
> call.setParams(params);
>
>
> URL url = new URL
> ("http://localhost:8080/soap/servlet/rpcrouter");
> Response resp = call.invoke (/* router URL */ url,
> /* actionURI */ "" );
>
> // Check the response.
> if (resp.generatedFault ()) {
> Fault fault = resp.getFault ();
> System.out.println ("Ouch, the call failed:
> "+fault);
> System.out.println (" Fault Code = " +
> fault.getFaultCode ());
> System.out.println (" Fault String = " +
> fault.getFaultString ());
> }
> else
> {
> Parameter result = resp.getReturnValue ();
> Object value = result.getValue();
> samplebean smpbn = (samplebean)value;
> System.out.println ("Result is " + smpbn.name +
> value);
> }
> }
> catch(Exception e)
> {
> System.out.println("Exception " +e.getMessage());
> }
>
> }
>
> }
>
> ------------------------------------------------------------------
> ----------
> ------------------------------------------------------------------
> ----------
> ------------------------------------------------------------------
> ----------
> ----------------------
> The server (service object ) code
> ------------------------------------------------------------------
> ----------
> ------------------------------------------------------------------
> ----------
> ------------------------------------------------------------------
> ----------
> ----------------------
> import java.util.*;
> import org.w3c.dom.*;
> import javax.xml.parsers.*;
> import org.apache.soap.util.xml.*;
> import samplebean;
>
> public class SampleServer
> {
>
> public SampleServer(){
> }
> public samplebean samplemethod(String str)
> {
> samplebean sb = new samplebean();
> try
> {
> System.out.println( "Received from client " +str );
>
> sb.name = "raja";
>
>
> }
> catch ( Exception e)
> {
> System.out.println ("Server " +e.getMessage());
> }
> finally
> {
> return sb ;
> }
> }
> }
> ------------------------------------------------------------------
> ----------
> ------------------------------------------------------------------
> ----------
> ------------------------------------------------------------------
> ----------
> ----------------------
>
> The bean class
> ------------------------------------------------------------------
> ----------
> ------------------------------------------------------------------
> ----------
> ------------------------------------------------------------------
> ----------
> ----------------------
> public class samplebean
> {
> public samplebean()
> {
> }
> public String name = new String();
> public int age;
>
> public String getname()
> {
> return name;
> }
> }
> ------------------------------------------------------------------
> ----------
> ------------------------------------------------------------------
> ----------
> ------------------------------------------------------------------
> ----------
> ----------------------
>
> Please suggest. Thanx in advance.
> Raja
> Innova Solutions
> Ph: Off : 3543139,40,50 X 217
> Res : 3551023
>