最近开始研究SOAP,下面是参照别人的例子,在别人的例子基础上 成功实现的一个例子,具体详细信息要参照:
soap中的docs
第一步、配置好tomcat,j2se
第二步、下载 Apache SOAP() ,减压,将其中的soap.war拷贝到tomaca/webapps/下
第三步、启动tomcat,此时tomcat会自动部署soap,启动成功后,将soap.war删掉
第五步、下载mail.jar,soap.jar放入环境变量中(设置calaapath中)
第四步、编写服务类HelloSOAP.java如下:
public class HelloSOAP
{
public String getWelcomeValue(String name)
{
String strResult = "欢迎光临SOAP:" + name;
return strResult;
}
}
编译生成的HelloSOAP.class放在soap/WEB-INF/classes下
第五步、编写客户类:HelloSOAPClient.java如下:
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 = "this is a test";
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();
}
}
}
编译生成的HelloSOAPClient.class放在soap/WEB-INF/classes下
第六步、创建soap/WEB-INF/lib目录,下载mail.jar,soap.jar放入其中
第七步、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
第八步、运行,java HelloSOAPClient
可以看到返回值信息如下:
SOAP调用测试开始。。。
Name:this is a test
Result:欢迎光临SOAP:this is a test
SOAP调用测试结束。。。
则证明配置和测试成功,以后就可以发布自己的SOAP服务了!
第九步、删除服务Un-deploy之即可
Un-Deploy a Service
Select the service to be undeployed: