permaculture playing cards*
The moose likes Web Services and the fly likes help on SOAP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Web Services
Bookmark "help on SOAP" Watch "help on SOAP" New topic
Author

help on SOAP

midan myaa
Greenhorn

Joined: Nov 26, 2001
Posts: 8
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
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;
}
}


Angela Margot <img src="smile.gif" border="0"> <br />Sr. Software Engineer<br />SCJP2 SCWCD MCSD MCP
Angela Margot
Ranch Hand

Joined: Feb 07, 2002
Posts: 80
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
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
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
Angela Margot
Ranch Hand

Joined: Feb 07, 2002
Posts: 80
There is a good 4 part tutorial on SOAP messaging with code examples - parts 2-4 were the best:
Part1:
http://www-106.ibm.com/developerworks/webservices/library/ws-peer1/?dwzone=ws
Part2:
http://www-106.ibm.com/developerworks/webservices/library/ws-peer2/?dwzone=ws
Part3:
http://www-106.ibm.com/developerworks/webservices/library/ws-peer3/?dwzone=ws
Part4:
http://www-106.ibm.com/developerworks/webservices/library/ws-peer4/?dwzone=ws
Maybe these might help?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
What an amazing statement!

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

Are you just looking for the text of a SOAP message or what???
Bill
 
jQuery in Action, 2nd edition
 
subject: help on SOAP
 
Similar Threads
InvocationTargetException when transferring a custom class in inheritance
what is SOAP and what for it is used?
Consuming SOAP Web Service
validating soap response
Print Soap request/response XMLs