发布WebService常用框架.docx
- 文档编号:30158444
- 上传时间:2023-08-05
- 格式:DOCX
- 页数:11
- 大小:18.96KB
发布WebService常用框架.docx
《发布WebService常用框架.docx》由会员分享,可在线阅读,更多相关《发布WebService常用框架.docx(11页珍藏版)》请在冰豆网上搜索。
发布WebService常用框架
首先,介绍一下几种常用的webservice开发框架的开发步骤。
Net环境
IDE为MicrosoftVisualStudio2005
Ø 服务器端开发步骤为:
1、 新建一个WebSite,选择ASP.NetWebService,语言选择C#
2、 在IDE自动生成的服务器端代码App_Code/Service.cs中加入自己的业务逻辑代码
3、 选中WebSite,右键选择“ViewinBrowser”即可进行测试
VS2005自带有内嵌的测试web服务器,测试起来非常方便。
注意Service.asmx为webservice发布后的访问入口文件。
要发布到IIS或者apache中,需要先选中WebSite,右键选择“PublishWebSite”,指定发布的目录,即对应的web服务器发布应用的目录。
Ø 客户端开发步骤:
1、 新建一个ConsoleApplication
2、 选中工程,右键选择“AddWebReference”,指定可访问的webservice的URL地址,进行添加
3、 引入命名空间,即可方便使用客户端存根代码。
Java环境
CXF
CXF的前身是Xfire,具体内容可参照http:
//xfire.codehaus.org/。
IDE为Eclipse3.2(JDK1.5以上)
Ø 服务器端开发步骤为:
1、 首先访问http:
//incubator.apache.org/cxf/下载所需的jar包。
2、 新建一个web工程,将下载的CXF的开发包加入到lib中。
3、 在web.xml文件中添加提供webservice的监听servlet,对应的处理类名为org.apache.cxf.transport.servlet.CXFServlet,并设置其启动时装载顺序属性为1;指定访问servlet的URL格式。
内容可参考如下所示:
xmlversion="1.0"encoding="UTF-8"?
>
xmlns: xsi="http: //www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation=" --cxfservletconfiguration--> org.apache.cxf.transport.servlet.CXFServlet 4、 新建一个services.xml配置文件,内容如下所示: xmlversion="1.0"encoding="UTF-8"? > //www.springframework.org/schema/beans" xmlns: xsi="http: //www.w3.org/2001/XMLSchema-instance" xmlns: jaxws="http: //cxf.apache.org/jaxws" xmlns: simple="http: //cxf.apache.org/simple" xmlns: soap="http: //cxf.apache.org/bindings/soap" xsi: schemaLocation=" http: //www.springframework.org/schema/beanshttp: //www.springframework.org/schema/beans/spring-beans-2.0.xsd http: //cxf.apache.org/bindings/soaphttp: //cxf.apache.org/schemas/configuration/soap.xsd http: //cxf.apache.org/simplehttp: //cxf.apache.org/schemas/simple.xsd http: //cxf.apache.org/jaxwshttp: //cxf.apache.org/schemas/jaxws.xsd"> META-INF/cxf/cxf.xml"/> META-INF/cxf/cxf-extension-soap.xml"/> META-INF/cxf/cxf-servlet.xml"/> endpointid="NotifyService"implementor="#notifyService" address="/NotifyService"/> 在如上的配置文件中,我们指定了要发布为webservice的类名,访问地址,访问端点标识。 5、 编写要发布为webservice的类和接口,注意注解(annotation)的使用。 可参照如下的代码: packagecom.webservice; importjavax.jws.WebService; //接口类: @WebService publicinterfaceINotifyService{ publicvoidsayHello(Stringname); } //实现类: packagecom.webservice; @WebService publicclassNotifyServiceimplementsINotifyService{ publicvoidsayHello(Stringname){ System.out.println(“hello,”+name); } } 6、 向lib包中加入Spring的开发包,并确保classpath下有对应的bean定义文件,例如beans.xml。 xmlversion="1.0"encoding="UTF-8"? > DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN""http: //www.springframework.org/dtd/spring-beans-2.0.dtd"> 7、 发布web应用到对应的web容器(例如tomcat5.5)或者用内嵌的Jetty6.0进行部署。 Ø 客户端的开发步骤: 这里介绍通过wsdl文件生成客户端存根代码的方式,前提是要先下载CXF的工具包。 1、 编辑Ant脚本,可参照如下配置: xmlversion="1.0"? > 用于通过wsdl文件生成java代码的ant文件 /2008/cxf\apache-cxf-2.0.3-incubator"/> 上面的WebService.wsdl为服务器端webservice对应的wsdl文件。 2、 运行Ant脚本生成客户端存根代码,将代码和依赖的CXF包引入到自己的工程即可使用。 NetBeans 由于Sun已经在J2EE5.0中提供了对Jax-ws规范的实现,并且提供了NetBeansIDE对J2EE5.0的支持,使得开发webservice非常简单。 前提条件是我们需要到beans.org/下载NetBeans6.0。 官方的下载文件中已经集成了Tomcat6.0和J2EE5.0。 Ø 服务器端开发步骤: 1、 打开NetBeansIDE,新建一个WebApplication 2、 选中WebApplication,右键选择“New――WebService”,按照向导和图形界面新建自己的webservice。 注意对Jax-ws中注解的使用,可参照如下代码: packagecom.webservice; importjava.util.Calendar; importjavax.jws.WebMethod; importjavax.jws.WebParam; importjavax.jws.WebService; importjavax.xml.ws.Holder; @WebService() publicclassnotify{ @WebMethod(operationName="SendSMS") publicbooleanSendSMS( @WebParam(name="userID")StringuserID, @WebParam(name="systemID")StringsystemID, @WebParam(name="content")Stringcontent, @WebParam(name="sendSMSID")StringsendSMSID){ //TODOwriteyourimplementationcodehere: System.out.println(userID); System.out.println(systemID); System.out.println(content); System.out.println(sendSMSID); returnfalse; } @WebMethod(operationName="ReceiveSMS") publicnet.gmcc.sz.eap.WebService.SMSResponse[]ReceiveSMS( @WebParam(name="systemID")StringsystemID, @WebParam(name="autoClose")booleanautoClose){ //TODOwriteyourimplementationcodehere: System.out.println(systemID); System.out.println(autoClose); SMSResponse[]responses=newSMSResponse[3]; for(inti=0;i<3;i++){ SMSResponseresp=newSMSResponse(); resp.setContent("content: "+i); resp.setSendSMSID("sendSMSID: "+i); resp.setReceivedSMSID("receivedSMSID"+i); responses[i]=resp; } returnresponses; } @WebMethod(operationName="SendSMS_Time") publicbooleanSendSMSTime( @WebParam(name="userID")StringuserID, @WebParam(name="systemID")StringsystemID, @WebParam(name="content")Stringcontent, @WebParam(name="sendTime")CalendarsendTime, @WebParam(name="sendSMSID")StringsendSMSID){ //TODOwriteyourimplementationcodehere: System.out.println(userID); System.out.println(systemID); System.out.println(content); System.out.println(sendTime); System.out.println(sendSMSID); returnfalse; } @WebMethod(operationName="sendByteData") publicbooleansendByteData( @WebParam(name="data")byte[]data){ //TODOwriteyourimplementationcodehere: System.out.println(newString(data)); returnfalse; } /** *Webserviceoperation */ @WebMethod(operationName="sendRefParameter") publicStringsendRefParameter( @WebParam(name="content",mode=WebParam.Mode.INOUT)Holder //TODOwriteyourimplementationcodehere: content.value="serverreceived"; System.out.println(content.value); returncontent.value; } } Ø 客户端开发: 1、 打开NetBeansIDE,新建一个JavaApplication 2、 选中JavaApplication,右键Newwebserviceclient,按照向导完成 3、 在生成的Main类的方法中,右键――>Webserviceclientresource――>Callwebserviceoperation,按照向导完成 4、 设置调用参数的初始值,完成客户端的调用代码 Axis 这里简单介绍一下Axis1.x版本的使用方法。 Ø 服务器端开发: 1、 到http: //ws.apache.org/axis/下载所需的lib包 2、 将lib包中的jar包路径设置到classpath中 3、 启动web服务器,发布下载包中的web应用axis;访问http: //127.0.0.1: 8080/axis/测试是否访问正常 4、 编写deploy.wsdd文件,参照下面: //xml.apache.org/axis/wsdd/" xmlns: java="http: //xml.apache.org/axis/wsdd/providers/java"> RPC"> 5、 使用命令行进行部署: java-cp%AXISCLASSPATH%org.apache.axis.client.AdminClient -lhttp: //localhost: 8080/axis/services/AdminServicedeploy.wsdd Ø 客户端开发: 1、 下载lib包,加入到classpath 2、 获得发布为webservice的wsdl文件 3、 使用命令行生成客户端存根代码: javaorg.apache.axis.wsdl.WSDL2Javawebservice.wsdl 4、 将存根代码导入工程,引入依赖的lib包即可使用。 比较 Ø 开发过程比较 1、 NetBeansIDE基本可以与.Net的开发工具VS2005相媲美,后者有内嵌的测试服务器,前者也可以使用内嵌的glassfish。 2、 CXF相对而言也比较简单(依赖于与Spring的集成),Axis要更复杂些,且难以掌握 Ø 性能比较 1、 CXF对于大数据量的处理,性能要优于NetBeansIDE开发的webservice;小数据量的处理两者差不多。 2、 对于.Net开发的webservice的性能很难与Java环境下开发的webservice进行性能比较,由于应用服务器不相同(前者依赖于IIS的支持)。 Ø 结论 考虑到服务的性能和现有应用的整合,使用CXF进行webservice开发是比较适中的选择。 从简单易用方面考虑,可以使用NetBeans进行上手入门。 从与之前作的webservice的兼容方面考虑,可以采用Axis,只有它还支持RPC的调用方式。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 发布 WebService 常用 框架
![提示](https://static.bdocx.com/images/bang_tan.gif)