本文主要参考:http://blog.csdn.net/xw13106209/article/details/7049614,并对原文几处小小的谬误进行了改正和补充。
1.系统功能:
- 安装Eclipse-jee;
- 下载最新版本的Axis2,网址http://axis.apache.org/axis2/java/core/download.cgi ,选择Standard Binary Distribution的zip包,解压缩得到的目录名axis2-1.4.1,目录内的文件结构如下:
3.开发前配置:
在Eclipse的菜单栏中,Window --> Preferences --> Web Service --> Axis2 Perferences,在Axis2 runtime location中选择Axis2解压缩包的位置,设置好后,点"OK"即行。(如图)
4.开发Web Service:
(1)新建一个Java Project,命名为"WebServiceTest1"
(2)新建一个class,命名为"CalculateService",完整代码如下:
package edu.sjtu.webservice; /** * 计算器运算 * @author rongxinhua */ public class CalculateService { //加法 public float plus(float x, float y) { return x + y; } //减法 public float minus(float x, float y) { return x - y; } //乘法 public float multiply(float x, float y) { return x * y; } //除法 public float divide(float x, float y) { if(y!=0) { return x / y; } else return -1; } }(3)在"WebServiceTest1"项目上new --> other,找到"Web Services"下面的"Web Service";
(4)下一步(next),在出现的Web Services对象框,在Service implementation中点击"Browse",进入Browse Classes对象框,查找到我们刚才写的写的CalculateService类。(如下图)。点击"ok",则回到Web Service话框。
(5)在Web Service对话框中,将Web Service type中的滑块,调到"start service“的位置,将Client type中的滑块调到"Test client"的位置。
(6)在Web Service type滑块图的右边有个"Configuration",点击它下面的选项,进入Service Deployment Configuration对象框,在这里选择相应的Server(我这里用Tomcat6.0)和Web Service runtime(选择Apache Axis2),如下图:
*此处我最开始用的是tomcat7,但是出现了如下错误:
原因是Axis2不支持Servlet3.0,所以应该使用Servlet3.0以下规范,如2.5,2.4。由于tomcat6支持的是servlet2.5,tomcat7支持的是servlet3,所以此处我的解决方案是把tomcat6换成tomcat7。由于我的eclipse里面只配置了tomcat7,所以需要重新配置tomcat6。详细操作见Iteye的收藏:Eclipse配置Tomcat,http://412887952-qq-com.iteye.com/blog/1374907
(7)点OK后,则返回到Web Service对话框,同理,Client type中的滑块右边也有"Configuration",也要进行相应的设置,步骤同上。完成后,Next --> next即行。进入到Axis2 Web Service Java Bean Configuration,我们选择Generate a default services.xml,如下图所示:
(8)到了Server startup对话框,有个按键"start server"(如下图),点击它,则可启动Tomcat服务器了。
*此时不启动服务器,可以在server控制台启动tomcat6服务器,然后webService就可以运行了
(9)等启完后,点击"next“,此时点击Launch可以从浏览器访问,记下该地址,每个人的端口可能不一样,所以地址也不一样。本机为:http://127.0.0.1:65396/wse/wsexplorer/wsexplorer.jsp?org.eclipse.wst.ws.explorer=5
点击图标进入WSDL Page。再输入已发布的WebService wsdl的地址则可以看到如(10)中的图效果
(10)”next-- > next",一切默认即行,最后,点击完成。最后,出现如下界面:(Web Service Explorer),我们在这里便可测试我们的Web服务。(使用浏览器打开的话使用如下地址:http://127.0.0.1:65396/wse/wsexplorer/wsexplorer.jsp?org.eclipse.wst.ws.explorer=5,每机不同)。如下图所示:
注:在浏览器中打开Web Service Explorer(有时候在eclipse中关闭了webservice explorer,可以用这种方法打开)
首先登录地址:http://127.0.0.1:65396/wse/wsexplorer/wsexplorer.jsp?org.eclipse.wst.ws.explorer=5。然后在网页右上角选择WSDL Page标签。然后输入WSDL地址:http://localhost:8080/WebServiceTest1/services/CalculateService?wsdl 。这个wsdl地址就是我们刚才发布服务的那个wsdl。点击go,如下图所示:
然后就可以看到如下界面了:
(10)测试比较简单,例如,我们选择一个"plus"的Operation(必须是CalculateServiceSoap11Binding),出现下图,在x的输入框中输入1,在y的输入框中输入2,点击"go",便会在status栏中显示结果3.0。其他方法的测试也类似。结果如上图所示。
5.CalculateService客户端调用程序
package edu.sjtu.webservice.test; import javax.xml.namespace.QName; import org.apache.axis2.AxisFault; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.rpc.client.RPCServiceClient; public class CalculateServiceTest { /** * @param args * @throws AxisFault */ public static void main(String[] args) throws AxisFault { // TODO Auto-generated method stub // 使用RPC方式调用WebService RPCServiceClient serviceClient = new RPCServiceClient(); Options options = serviceClient.getOptions(); // 指定调用WebService的URL EndpointReference targetEPR = new EndpointReference( "http://localhost:8080/WebServiceTest1/services/CalculateService"); options.setTo(targetEPR); // 指定要调用的计算机器中的方法及WSDL文件的命名空间:edu.sjtu.webservice。此处的命 //名空间必须是服务器中要发布的类的package名,否则会报 namespace mismatch的错 QName opAddEntry = new QName("http://webservice.sjtu.edu","plus");//加法 QName opAddEntryminus = new QName("http://webservice.sjtu.edu","minus");//减法 QName opAddEntrymultiply = new QName("http://webservice.sjtu.edu","multiply");//乘法 QName opAddEntrydivide = new QName("http://webservice.sjtu.edu","divide");//除法 // 指定plus方法的参数值为两个,分别是加数和被加数 Object[] opAddEntryArgs = new Object[] { 1,2 }; // 指定plus方法返回值的数据类型的Class对象 Class[] classes = new Class[] { float.class }; // 调用plus方法并输出该方法的返回值 System.out.println(serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0]); System.out.println(serviceClient.invokeBlocking(opAddEntryminus,opAddEntryArgs, classes)[0]); System.out.println(serviceClient.invokeBlocking(opAddEntrymultiply,opAddEntryArgs, classes)[0]); System.out.println(serviceClient.invokeBlocking(opAddEntrydivide,opAddEntryArgs, classes)[0]);*客户端必须导入以下jar包:
异常: java.lang.ClassNotFoundException: org.apache.axis2.transport.local.LocalTransportSender 缺少: axis2-transport-local-1.5.2.jar
异常:java.lang.ClassNotFoundException:org.apache.axis2.transport.http.CommonsHTTPTransportSender
缺少:axis2-transport-http-1.5.2.jar
异常:java.lang.NoClassDefFoundError: javax/mail/internet/ParseException
缺少:mail-1.4.jar
异常:java.lang.NoClassDefFoundError: org/apache/http/HttpResponseFactory
缺少:httpcore-4.0.jar