package hello; public class HelloServer { public String sayHelloTo(String name) { System.out.println("sayHelloTo(String name)"); return "Hello " + name + ", How are you doing?"; } public String sayHelloTo(Name theName) { System.out.println("sayHelloTo(Name theName)"); return "Hello " + theName.getName() + ", How are you doing?"; } } |
package hello; public class Name { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } |
<isd:service xmlns:isd="http://xml.apache.org/xml-soap/deployment" id="urn:Hello"> <isd:provider type="java" scope="Application" methods="sayHelloTo"> <isd:java class="hello.HelloServer" static="false"/> </isd:provider> <isd:mappings> <isd:map encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:x="urn:Hello" qname="x:hello.Name" javaType="hello.Name" java2XMLClassName="org.apache.soap.encoding.soapenc.BeanSerializer" xml2JavaClassName="org.apache.soap.encoding.soapenc.BeanSerializer"/> </isd:mappings> </isd:service> |
// 创建类型映射注册器 SOAPMappingRegistry smr = new SOAPMappingRegistry(); BeanSerializer beanSer = new BeanSerializer(); // 映射类型 smr.mapTypes(Constants.NS_URI_SOAP_ENC, new QName("urn:Hello", "hello.Name"),hello.Name.class, beanSer, beanSer); |
call.setSOAPMappingRegistry(smr); |
// 设置调用参数 Vector params = new Vector(); Name theName = new Name(); theName.setName(name); params.addElement(new Parameter("name", hello.Name.class, theName, null)); call.setParams(params); |
Listing 3: Client2.java package hello; import java.net.URL; import java.util.Vector; import org.apache.soap.SOAPException; import org.apache.soap.Constants; import org.apache.soap.Fault; import org.apache.soap.rpc.Call; import org.apache.soap.rpc.Parameter; import org.apache.soap.rpc.Response; import org.apache.soap.encoding.SOAPMappingRegistry; import org.apache.soap.encoding.soapenc.BeanSerializer; import org.apache.soap.util.xml.QName; public class Client2 { public static void main(String[] args) throws Exception { if(args.length == 0) { System.err.println("Usage: java hello.Client [SOAP-router-URL] "); System.exit (1); } try { URL url = null; String name = null; if(args.length == 2) { url = new URL(args[0]); name = args[1]; } else { url = new URL("http://localhost:8080/apache-soap/servlet/rpcrouter"); name = args[0]; } // 构造调用对象 Call call = new Call(); call.setTargetObjectURI("urn:Hello"); call.setMethodName("sayHelloTo"); call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC); // 创建类型映射注册器 SOAPMappingRegistry smr = new SOAPMappingRegistry(); BeanSerializer beanSer = new BeanSerializer(); // 映射类型 smr.mapTypes(Constants.NS_URI_SOAP_ENC, new QName("urn:Hello", "hello.Name"), hello.Name.class, beanSer, beanSer); call.setSOAPMappingRegistry(smr); // 设置参数 Vector params = new Vector(); Name theName = new Name(); theName.setName(name); params.addElement(new Parameter("name", hello.Name.class, theName, null)); call.setParams(params); // 发出调用 Response resp = null; try { resp = call.invoke(url, ""); } catch( SOAPException e ) { System.err.println("Caught SOAPException (" + e.getFaultCode() + "): " + e.getMessage()); System.exit(-1); } // 检查应答 if( !resp.generatedFault() ) { Parameter ret = resp.getReturnValue(); Object value = ret.getValue(); System.out.println(value); } else { Fault fault = resp.getFault(); System.err.println("Generated fault: "); System.out.println (" Fault Code = " + fault.getFaultCode()); System.out.println (" Fault String = " + fault.getFaultString()); } } catch(Exception e) { e.printStackTrace(); } } } |
set CLASSPATH=E:\soap-2_0\samples\;E:\soap-2_0\lib\soap.jar;E:\xerces-1_2_0\xerces.jar javac -d .. HelloServer.java Client.java Client2.java |
set CLASSPATH=E:\soap-2_0\samples\;E:\soap-2_0\lib\soap.jar;E:\xerces-1_2_0\xerces.jar java hello.Client Tarak |
set CLASSPATH=E:\soap-2_0\samples\;E:\soap-2_0\lib\soap.jar;E:\xerces-1_2_0\xerces.jar java hello.Client2 Tarak |