查看文章 |
转自(http://gaoge2000.blog.hexun.com/15987048_d.html) 首先,介绍一下几种常用的web service开发框架的开发步骤。 Net环境 Ø 服务器端开发步骤为: 1、 新建一个Web Site,选择ASP.Net Web Service,语言选择C# 2、 在IDE自动生成的服务器端代码App_Code/Service.cs中加入自己的业务逻辑代码 3、 选中Web Site,右键选择“View in Browser”即可进行测试 VS2005自带有内嵌的测试web服务器,测试起来非常方便。注意Service.asmx为web service发布后的访问入口文件。 要发布到IIS或者apache中,需要先选中Web Site,右键选择“Publish Web Site”,指定发布的目录,即对应的web服务器发布应用的目录。 Ø 客户端开发步骤: 1、 新建一个Console Application 2、 选中工程,右键选择“Add Web Reference”,指定可访问的web service的URL地址,进行添加 3、 引入命名空间,即可方便使用客户端存根代码。 Java环境 IDE为Eclipse 3.2 (JDK 1.5以上) Ø 服务器端开发步骤为: 1、 首先访问http://incubator.apache.org/cxf/ 下载所需的jar包。 2、 新建一个web工程,将下载的CXF的开发包加入到lib中。 3、 在web.xml 文件中添加提供web service的监听servlet,对应的处理类名为org.apache.cxf.transport.servlet.CXFServlet,并设置其启动时装载顺序属性为1;指定访问servlet的URL格式。 内容可参考如下所示: <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- cxf servlet configuration --> <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class> org.apache.cxf.transport.servlet.CXFServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> </web-app>
4、 新建一个services.xml配置文件,内容如下所示:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://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/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/simple http://cxf.apache.org/schemas/simple.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <bean id="notifyService" class="bingo.mcs.service.ws.NotifyService" /> <jaxws:endpoint id="NotifyService" implementor="#notifyService" address="/NotifyService" /> </beans> 在如上的配置文件中,我们指定了要发布为web service的类名,访问地址,访问端点标识。 5、 编写要发布为web service的类和接口,注意注解(annotation)的使用。 可参照如下的代码:
package com.webservice;
import javax.jws.WebService; //接口类: @WebService public interface INotifyService { public void sayHello(String name); } //实现类: package com.webservice; @WebService public class NotifyService implements INotifyService { public void sayHello(String name){ System.out.println(“hello,”+name); } }
6、 向lib包中加入Spring的开发包,并确保classpath下有对应的bean定义文件,例如beans.xml。 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <beans>
</beans>
7、 发布web应用到对应的web容器(例如tomcat5.5)或者用内嵌的Jetty6.0进行部署。
Ø 客户端的开发步骤: 这里介绍通过wsdl文件生成客户端存根代码的方式,前提是要先下载CXF的工具包。 1、 编辑Ant脚本,可参照如下配置: <?xml version="1.0"?> <project name="wsdl2java" basedir="."> <description> 用于通过wsdl文件生成java代码的ant文件 </description> <property name="cxf.home" location="E:/2008/cxf\apache-cxf-2.0.3-incubator" /> <path id="cxf.classpath"> <fileset dir="${cxf.home}/lib"> <include name="*.jar" /> </fileset> </path> <target name="cxfWSDLToJava"> <java classname="org.apache.cxf.tools.wsdlto.WSDLToJava" fork="true"> <arg value="-client" /> <arg value="-d" /> <arg value="src/main" /> <arg value="WebService.wsdl" /> <classpath> <path refid="cxf.classpath" /> </classpath> </java> </target> </project> 上面的WebService.wsdl为服务器端web service对应的wsdl文件。 2、 运行Ant脚本生成客户端存根代码,将代码和依赖的CXF包引入到自己的工程即可使用。 NetBeans 前提条件是我们需要到http://www.netbeans.org/ 下载NetBeans 6.0。官方的下载文件中已经集成了Tomcat6.0和J2EE5.0。
Ø 服务器端开发步骤: 1、 打开NetBeans IDE,新建一个 Web Application 2、 选中Web Application,右键选择“New ――Web Service”,按照向导和图形界面新建自己的web service。 注意对Jax-ws中注解的使用,可参照如下代码: package com.webservice;
import java.util.Calendar; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; import javax.xml.ws.Holder;
@WebService() public class notify {
@WebMethod(operationName = "SendSMS") public boolean SendSMS(
@WebParam(name = "userID") String userID,
@WebParam(name = "systemID") String systemID,
@WebParam(name = "content") String content,
@WebParam(name = "sendSMSID") String sendSMSID) { //TODO write your implementation code here: System.out.println(userID); System.out.println(systemID); System.out.println(content); System.out.println(sendSMSID); return false; }
@WebMethod(operationName = "ReceiveSMS") public net.gmcc.sz.eap.WebService.SMSResponse[] ReceiveSMS( @WebParam(name = "systemID") String systemID, @WebParam(name = "autoClose") boolean autoClose) { //TODO write your implementation code here: System.out.println(systemID); System.out.println(autoClose); SMSResponse[] responses=new SMSResponse[3]; for(int i=0;i<3;i++){ SMSResponse resp=new SMSResponse(); resp.setContent("content:"+i); resp.setSendSMSID("sendSMSID:"+i); resp.setReceivedSMSID("receivedSMSID"+i); responses[i]=resp; } return responses; }
@WebMethod(operationName = "SendSMS_Time") public boolean SendSMSTime( @WebParam(name = "userID") String userID, @WebParam(name = "systemID") String systemID, @WebParam(name = "content") String content, @WebParam(name = "sendTime") Calendar sendTime, @WebParam(name = "sendSMSID") String sendSMSID) { //TODO write your implementation code here: System.out.println(userID); System.out.println(systemID); System.out.println(content); System.out.println(sendTime); System.out.println(sendSMSID); return false; }
@WebMethod(operationName = "sendByteData") public boolean sendByteData( @WebParam(name = "data") byte[] data) { //TODO write your implementation code here: System.out.println(new String(data)); return false; }
/** * Web service operation */ @WebMethod(operationName = "sendRefParameter") public String sendRefParameter( @WebParam(name = "content", mode = WebParam.Mode.INOUT) Holder<String> content) { //TODO write your implementation code here: content.value = "server received"; System.out.println(content.value); return content.value; } } Ø 客户端开发: 1、 打开NetBeans IDE,新建一个Java Application 2、 选中Java Application,右键New web service client,按照向导完成 3、 在生成的Main类的方法中,右键――>Web service client resource――>Call webservice operation,按照向导完成 4、 设置调用参数的初始值,完成客户端的调用代码 Axis Ø 服务器端开发: 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文件,参照下面: <deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> <service name="MyService" provider="java:RPC"> <parameter name="className" value="samples.userguide.example3.MyService"/> <parameter name="allowedMethods" value="*"/> </service> </deployment>
5、 使用命令行进行部署: java -cp %AXISCLASSPATH% org.apache.axis.client.AdminClient -lhttp://localhost:8080/axis/services/AdminService deploy.wsdd
Ø 客户端开发: 1、 下载lib包,加入到classpath 2、 获得发布为webservice的wsdl文件 3、 使用命令行生成客户端存根代码: java org.apache.axis.wsdl.WSDL2Java webservice.wsdl 4、 将存根代码导入工程,引入依赖的lib包即可使用。 比较 1、 NetBeans IDE基本可以与.Net的开发工具VS 2005相媲美,后者有内嵌的测试服务器,前者也可以使用内嵌的glassfish。 2、 CXF相对而言也比较简单(依赖于与Spring的集成),Axis要更复杂些,且难以掌握
Ø 性能比较 1、 CXF对于大数据量的处理,性能要优于NetBeans IDE开发的webservice;小数据量的处理两者差不多。 2、 对于.Net开发的webservice的性能很难与Java环境下开发的webservice进行性能比较,由于应用服务器不相同(前者依赖于IIS的支持)。 Ø 结论 考虑到服务的性能和现有应用的整合,使用CXF进行web service开发是比较适中的选择。从简单易用方面考虑,可以使用NetBeans进行上手入门。从与之前作的webservice的兼容方面考虑,可以采用Axis,只有它还支持RPC的调用方式。
|