原来发贴在CJSDN,后转载到Matrix,还是自己收集到Blog里来,由于SOAP停止开,接下来准备写个AXIS的开发。
http://www.cjsdn.net/post/view?bid=5&id=133408&sty=2
http://www.matrix.org.cn/thread.shtml?topicId=16780&forumId=13
Apache SOAP的Web Services在Tomcat上的初体验:安装和测试
下载:
Apache SOAP http://xml.apache.org/soap/index.html
Tomcat http://jakarta.apache.org/
1、解压Apache SOAP到%ApacheSOAP%,如:F:/OpenSource/SOAP
2、安装Tomcat到%Tomcat%,如:F:/Program Files/Apache Group/Tomcat
3、复制%ApacheSOAP%/webapps/soap.war文件到%Tomcat%/webapps目录下
4、启动Tomcat,此时Tomcat自动将soap.war解压了
5、在%Tomcat%/webapps/soap/WEB-INF/classes建立测试文件HelloSOAPClient.java、HelloSOAP.java,源码附后
6、编译,进入%Tomcat%/webapps/soap/WEB-INF/classes目录
javac -classpath .;%classpath%;F:/OpenSource/SOAP/lib/soap.jar *.java
7、SOAP服务的发布
使用IE浏览器浏览http://localhost:8080/soap/admin/index.html,进入Deploy,填写如下信息:
ID: urn:hello.hellosoap
Scope: Request
Methods getWelcomeValue
Provider Type: Java
Java Provider Provider Class: HelloSOAP
Java Provider Static: NO
确认发布,可以点List查看已发布服务
Service Listing
Here are the deployed services (select one to see details)
urn:hello.hellosoap
8、运行,java -classpath .;%classpath%;F:/OpenSource/SOAP/lib/soap.jar HelloSOAPClient
可以看到返回值信息如下:
SOAP调用测试开始。。。
Name:俞黎敏
Result:欢迎光临SOAP:俞黎敏
SOAP调用测试结束。。。
则证明配置和测试成功,以后就可以发布自己的SOAP服务了!
9、删除服务Un-deploy之即可
Un-Deploy a Service
Select the service to be undeployed:
urn:hello.hellosoap
public class HelloSOAP
{
public String getWelcomeValue(String name)
{
String strResult = "欢迎光临SOAP:" + name;
return strResult;
}
}
import java.net.URL;
import java.util.Vector;
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;
public class HelloSOAPClient
{
public static void main(String[] args) throws Exception
{
System.out.println("SOAP调用测试开始。。。");
String name = "俞黎敏";
HelloSOAPClient helloSOAPClient = new HelloSOAPClient();
//调用远程的SOAP服务
String resultValue = helloSOAPClient.doSOAPRequest("hello.hellosoap","getWelcomeValue","name",name);
System.out.println("Name:" + name);
System.out.println("Result:" + resultValue);
System.out.println("SOAP调用测试结束。。。");
}
public String doSOAPRequest(String _strURI,String _strMethodName,String _strName,String _strValue) throws Exception
{
//创建一个远程调用
Call call = new Call();
//设置远程对象的URI
call.setTargetObjectURI("urn:" + _strURI);
//设置调用的方法名
call.setMethodName(_strMethodName);
//设置编码风格
call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
//设置方法调用的参数
Vector params = new Vector();
params.addElement(new Parameter(_strName,String.class,_strValue,null));
call.setParams(params);
//发送RPC请求,请查看webapps/soap/web.xm,就知道rpcrouter是映射到org.apache.soap.server.http.RPCRouterServlet这个类
Response resp = call.invoke(new URL("http://localhost:8080/soap/servlet/rpcrouter"),"");
if(resp.generatedFault())
{
//远程调用出错处理
Fault fault = resp.getFault();
System.out.println("调用失败!");
System.out.println("错误代号:" + fault.getFaultCode());
System.out.println("错误信息:" + fault.getFaultString());
return "调用失败!";
}
else
{
//调用成功,获取返回值
Parameter result = resp.getReturnValue();
return result.getValue().toString();
}
}
}