免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1413 | 回复: 0
打印 上一主题 下一主题

Eclipse&Tomcat下的SOAP实现 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-07-22 21:52 |只看该作者 |倒序浏览
1.需要的jar包:activation.jar mail.jar soap.jar xercesImpl.jar xercesSamples.jar xml-apis.jar xmlParserAPIs.jar

2.新建web service工程,取名soap,目录树如下,其中我们将\soap-2_3_1\webapps\soap\下的几个文件copy到了工程的webroot下.
   


3.用到的源文件:

//HelloService.java
package org.eleaf.soap;
import java.util.Date;
public class HelloService {
public String sayHello(String username)
{
  return "Hello this is binger:" + username;
}
public Date getDate()
{
  return new Date();
}
public int sum(int i, int j)
{
  return i+j;
}
}




//HelloClient.java
package org.eleaf.soap;
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 HelloClient {
/**
  * @param args
  */
public static void main(String[] args) throws Exception{
  URL url = new URL("

http://localhost:8080/soap/servlet/rpcrouter
");
  //localhost可以改为服务所在的ip地址
  
  Call call = new Call();
  call.setTargetObjectURI("urn:helloservice");
  call.setMethodName("sayHello");
  call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
  Vector params = new Vector();
  params.addElement(new Parameter("username", String.class, "bitan", null));
  call.setParams(params);
  Response resp = call.invoke(url, "");
  if (resp.generatedFault())
  {
   Fault fault = resp.getFault();
   System.out.println("[code:" + fault.getFaultCode() +
     "];[description:" + fault.getFaultString() + "]");
  }
  else
  {
   Parameter result = resp.getReturnValue();
   System.out.println("[value:" + result.getValue() + "];[name:" +
     result.getName() + "];[class:" + result.getClass() +
     "];[type:" + result.getType() + "];[encoding:" +
     result.getEncodingStyleURI() + "]");
  }
  call.setMethodName("getDate");
  call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
  call.setParams(null);
  resp = call.invoke(url, "");
  if (resp.generatedFault())
  {
   Fault fault = resp.getFault();
   System.out.println("[code:" + fault.getFaultCode() +
     "];[description:" + fault.getFaultString() + "]");
  }
  else
  {
   Parameter result = resp.getReturnValue();
   System.out.println("[value:" + result.getValue() + "];[name:" +
     result.getName() + "];[class:" + result.getClass() +
     "];[type:" + result.getType() + "];[encoding:" +
     result.getEncodingStyleURI() + "]");
  }
   
  call.setMethodName("sum");
  call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
  params = new Vector();
  params.addElement(new Parameter("i", int.class, new Integer(24), null));
  params.addElement(new Parameter("j", int.class, new Integer(37), null));
  call.setParams(params);
  resp = call.invoke(url, "");
  if (resp.generatedFault())
  {
   Fault fault = resp.getFault();
   System.out.println("[code:" + fault.getFaultCode() +
     "];[description:" + fault.getFaultString() + "]");
  }
  else
  {
   Parameter result = resp.getReturnValue();
   System.out.println("[value:" + result.getValue() + "];[name:" +
     result.getName() + "];[class:" + result.getClass() +
     "];[type:" + result.getType() + "];[encoding:" +
     result.getEncodingStyleURI() + "]");
  }
}

}



//DeploymentDescriptor.xml
   
        
   

PS:这个文件手工编译的时候用,用soap自带的发布工具就不用了.


4.在tomcat上发布,然后在浏览器上输入:http://localhost:8080/soap/admin/  进入deploy页面,填入下面的信息:

ID
Scope
Request Session Application
Methods
(Whitespace separated list of method names)
Provider Type
Java Script User-Defined
For User-Defined Provider Type, Enter FULL Class Name:

发布后,进入到list页面可以看到一个名为:
urn:helloservice
  的服务,说明已经发布成功;

5.运行一下HelloClient,注意将里面的url改成服务所在的地址.会看到这样的response:

  [value:Hello this is binger:bitan];[name:return];[class:class org.apache.soap.rpc.Parameter];[type:class java.lang.String];[encoding:null]
[value:Tue Jul 22 21:15:19 CST 2008];[name:return];[class:class org.apache.soap.rpc.Parameter];[type:class java.util.Date];[encoding:null]
[value:61];[name:return];[class:class org.apache.soap.rpc.Parameter];[type:int];[encoding:null]


6.用WireShark抓包:XML格式的只能截图了,可我在这里没有传图片的空间了.将就着看吧
0000  00 16 c8 49 cf 43 00 1e  90 a1 b6 64 08 00 45 00   ...I.C.. ...d..E.
0010  03 77 31 0f 40 00 80 06  fc f2 3b 40 b3 c8 3b 40   .w1.@... ..;@..;@
0020  9f 36 1f 90 12 81 44 80  ac 43 7f 6f 8f 25 50 18   .6....D. .C.o.%P.
0030  fd cc f0 85 00 00 48 54  54 50 2f 31 2e 31 20 32   ......HT TP/1.1 2
0040  30 30 20 4f 4b 0d 0a 53  65 72 76 65 72 3a 20 41   00 OK..S erver: A
0050  70 61 63 68 65 2d 43 6f  79 6f 74 65 2f 31 2e 31   pache-Co yote/1.1
0060  0d 0a 53 65 74 2d 43 6f  6f 6b 69 65 3a 20 4a 53   ..Set-Co okie: JS
0070  45 53 53 49 4f 4e 49 44  3d 34 39 44 39 45 45 38   ESSIONID =49D9EE8
0080  30 33 32 43 32 46 46 32  31 45 38 32 35 38 35 34   032C2FF2 1E825854
0090  39 36 44 43 43 42 31 45  30 3b 20 50 61 74 68 3d   96DCCB1E 0; Path=
00a0  2f 73 6f 61 70 0d 0a 43  6f 6e 74 65 6e 74 2d 54   /soap..C ontent-T
00b0  79 70 65 3a 20 74 65 78  74 2f 78 6d 6c 3b 63 68   ype: tex t/xml;ch
00c0  61 72 73 65 74 3d 75 74  66 2d 38 0d 0a 43 6f 6e   arset=ut f-8..Con
00d0  74 65 6e 74 2d 4c 65 6e  67 74 68 3a 20 36 31 37   tent-Len gth: 617
00e0  0d 0a 44 61 74 65 3a 20  54 75 65 2c 20 32 32 20   ..Date:  Tue, 22
00f0  4a 75 6c 20 32 30 30 38  20 30 33 3a 32 32 3a 30   Jul 2008  03:22:0
0100  37 20 47 4d 54 0d 0a 43  6f 6e 6e 65 63 74 69 6f   7 GMT..C onnectio
0110  6e 3a 20 63 6c 6f 73 65  0d 0a 0d 0a 3c 3f 78 6d   n: close .......... ....urn:he lloservi
0330  63 65 3c 2f 69 74 65 6d  3e 0d 0a 3c 2f 72 65 74   ce............
            
  
7.出现的问题:

在linux下可以抓到本地请求和回应的http包,但是在windows下不行,所以,我让一哥们在另一台机子上布置了工程,向我的机子发送请求,这个问题还没搞懂为什么?有高手可以指点一下.

tomcat安装目录/conf/server.xml中网上有资料要加一句:


      ....







本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/14137/showart_1089155.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP