This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
can anyone please show me how to write a SOAP request and response that takes a persons name and say date of birth and provide some kind of YES/NO response..thanx.
Angela Margot
Ranch Hand
Joined: Feb 07, 2002
Posts: 80
posted
0
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; } }
Okay...I didn't set the smilies off -- so in one line you'll see a smiley -- it should be: String urn = "urn:Processmsg2"; Sorry about that!
Angela Margot
Ranch Hand
Joined: Feb 07, 2002
Posts: 80
posted
0
I FORGOT!! Also, you'll need to set up your service on the server. For example, we are using the Weblogic server. To set it up you need the following values entered in your admin tool: Property: VALUE ID: urn rocessmsg2 Scope: Application Provider type: java Java Provider class: test2.Leads (the package to Leads) Use Static Class: false Methods: receive (see in Leads) Type Mappings: ------------- Number of mappings: 1 encoding style: SOAP Namespace URI: urn:my_encoding2 (see Client2) Local part: newleads Java Type: test2.NewLeads (the new object) java to XML Serializer: org.apache.soap.encoding.soapenc.BeanSerializer XML to java Serializer: org.apache.soap.encoding.soapenc.BeanSerializer This is how I was set up using the Apache SOAP and a Weblogic server.
midan myaa
Greenhorn
Joined: Nov 26, 2001
Posts: 8
posted
0
Thank Leilani, but i'm not looking for an implementation i just want a SOAP request/response that will work..i hope you can still help me..thanx