SOAP的第一次体验

最近开始研究SOAP,下面是参照别人的例子,在别人的例子基础上 成功实现的一个例子,具体详细信息要参照:

soap中的docs

 

 

 

 

 

第一步、配置好tomcatj2se

第二步、下载 Apache SOAP() ,减压,将其中的soap.war拷贝到tomaca/webapps/

第三步、启动tomcat,此时tomcat会自动部署soap,启动成功后,将soap.war删掉

第五步、下载mail.jarsoap.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.jarsoap.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:
欢迎光临SOAPthis is a test
SOAP
调用测试结束。。。

则证明配置和测试成功,以后就可以发布自己的SOAP服务了!

第九步、删除服务Un-deploy之即可
Un-Deploy a Service
Select the service to be undeployed:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值