简介

WebService是一种服务的提供方式,通过WebService,不同应用间相互间调用变的很方便,网络上有很多常用的WebService服务,如:http://developer.51cto.com/art/200908/147125.htm,不同的语言平台对WebService都有实现,Java的WebService实现,比较流行的有Axis2、Jaxws,本文介绍的是Axis2。

Axis2下载和部署

Axis2是Apache开发的一个开源项目,再次感叹Apache的伟大!

下载地址:

http://mirror.bit.edu.cn/apache/axis/axis2/java/core/1.6.2/axis2-1.6.2-war.zip

将其内axis2.war解压到<Tomcat安装目录>/webapps下,启动Tomcat,war包会自动解压,

访问http://localhost:8080/axis2/,如果看到欢迎主页,则说明部署成功。

配置Axis2

<Tomcat安装目录>/webapps/axis2/WEB-INF/conf/axis2.xml,配置其内两个属性,以便调试。

  1. <parameter name="hotdeployment">true</parameter><!-- 开启热部署,不需要重启即可部署服务 -->
  2. <parameter name="hotupdate">true</parameter><!-- 开启热更新,不需要重启即可更新服务 -->

编写服务

所谓服务就是编写一个类,写一些方法,方法返回数据,WebService客户端获取数据。

  1. public class HelloService {
  2. public String sayHello() {
  3. return "hello";
  4. }
  5. }

0配置POJO发布服务

服务类创建好后,我们需要发布到服务器上,将HelloService.class放到<Tomcat安装目录>/webapps/axis2/WEB-INF/pojo下,pojo没有需要创建。

至此,我们已经成功的创建了一个WebService服务了,so easy!

再次访问http://localhost:8080/axis2/,点击Services,可以发现可用services中多了一个HelloService,其内有一个可用操作sayHello,说明发布成功。

  1. HelloService
  2. Service Description : No description available for this service
  3. Service EPR : http://localhost:8080/axis2/services/HelloService
  4. Service Status : Active
  5. Available Operations
  6. sayHello

访问http://localhost:8080/axis2/services/HelloService,页面输出正是我们的返回值。

  1. <ns:sayHelloResponse xmlns:ns="http://ws.apache.org/axis2">
  2. <return>hello</return>
  3. </ns:sayHelloResponse>

这里有两点需要注意:

- POJO发布的类不能放在包里,既不能使用package关键字;

- 默认的发布目录是pojo,可以在<Tomcat安装目录>/webapps/axis2/WEB-INF/conf/axis2.xml中增加目录,

  1. <deployer extension=".class" directory="<要增加的目录名称>" class="org.apache.axis2.deployment.POJODeployer" />

要注意多个目录间WebService要唯一,否则会重名,重名后,先部署的会成功,后部署的会报错。

services.xml配置文件发布服务

虽然POJO的方式不需要配置文件,但是其服务类不能放在包内,显然是不符合我们日常开发的,Axis2也允许带包的类发布WebService,如果不允许,估计就没人用了。

首先写一个较复杂的服务类,多个方法,带参数,有返回值的。

  1. package webservice.test;
  2. /**
  3. * 计算器运算
  4. *
  5. * @author gaoshuang
  6. */
  7. public class CalculateService {
  8. // 加法
  9. public float plus(float x, float y) {
  10. return x + y;
  11. }
  12. // 减法
  13. public float minus(float x, float y) {
  14. return x - y;
  15. }
  16. // 乘法
  17. public float multiply(float x, float y) {
  18. return x * y;
  19. }
  20. // 除法
  21. public float divide(float x, float y) {
  22. if (y != 0)
  23. return x / y;
  24. else
  25. return -1;
  26. }
  27. }

然后编写services.xml,该文件需要放在META-INF文件夹下。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- 服务名称 -->
  3. <service name="CalculateService">
  4. <!-- 服务描述 -->
  5. <description>
  6. 加减乘除计算服务
  7. </description>
  8. <!-- 设置服务类 -->
  9. <parameter name="ServiceClass">
  10. com.runqianapp.webservice.test.CalculateService
  11. </parameter>
  12. <!-- 方法 -->
  13. <operation name="plus">
  14. <!-- 方法处理器,RPCMessageReceiver为带返回值的处理器,
  15. RPCInOnlyMessageReceiver为不带返回值的处理器 -->
  16. <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
  17. class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
  18. </operation>
  19. <operation name="minus">
  20. <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
  21. class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
  22. </operation>
  23. <operation name="multiply">
  24. <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
  25. class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
  26. </operation>
  27. <operation name="divide">
  28. <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
  29. class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
  30. </operation>
  31. </service>

最后将这两个文件打成jar包,不论用工具还是手动打,打的都是最外层的文件夹。

我打的名字是server.jar,更改后缀为aar,所以最后是server.aar,Axis2建议使用aar发布WebService,

将server.aar放到<Tomcat安装目录>/webapps/axis2/WEB-INF/services下,访问http://localhost:8080/axis2/services/listServices

多出了一个CalculateService,说明发布成功。

  1. CalculateService
  2. Service Description : CalculateService
  3. Service EPR : http://localhost:8080/axis2/services/CalculateService
  4. Service Status : Active
  5. Available Operations
  6. divide
  7. plus
  8. minus
  9. multiply

分别访问

http://localhost:8080/axis2/services/CalculateService/plus?x=1&y=2

http://localhost:8080/axis2/services/CalculateService/divide?x=1&y=2

http://localhost:8080/axis2/services/CalculateService/minus?x=1&y=2

http://localhost:8080/axis2/services/CalculateService/multiply?x=1&y=2
       也可以发布多个WebService,可以使用serviceGroup标签。

  1. <serviceGroup>
  2. <service name="myService1">
  3. ...
  4. </service>
  5. <service name="myService2">
  6. ...
  7. </service>
  8. </serviceGroup>

客户端实现

以上介绍的都是WebService服务创建及发布,那么有了一个WebService服务后,我们如何调用呢?只在浏览器上访问是没有意义的。

下载Axis2客户端压缩包:http://mirror.esocc.com/apache/axis/axis2/java/core/1.6.2/axis2-1.6.2-bin.zip,并解压。

新建工程WebServiceClientTest,将<Axis2客户端安装目录>/lib下所有jar包添加到工程中;

编写客户端代码;

  1. package webservice.client.test;
  2. import javax.xml.namespace.QName;
  3. import org.apache.axis2.AxisFault;
  4. import org.apache.axis2.addressing.EndpointReference;
  5. import org.apache.axis2.client.Options;
  6. import org.apache.axis2.rpc.client.RPCServiceClient;
  7. public class Client1 {
  8. /**
  9. * @param args
  10. * @throws AxisFault
  11. */
  12. public static void main(String[] args) throws AxisFault {
  13. // 使用RPC方式调用WebService
  14. RPCServiceClient serviceClient = new RPCServiceClient();
  15. Options options = serviceClient.getOptions();
  16. // 指定调用WebService的URL
  17. EndpointReference targetEPR = new EndpointReference(
  18. "http://localhost:8080/axis2/services/CalculateService");
  19. options.setTo(targetEPR);
  20. // 调用方法的参数值
  21. Object[] entryArgs = new Object[] {1, 2};
  22. // 调用方法返回值的数据类型的Class对象
  23. Class[] classes = new Class[] { float.class };
  24. // 调用方法名及WSDL文件的命名空间
  25. // 命名空间是http://localhost:8080/axis2/services/CalculateService?wsdl中wsdl:definitions标签targetNamespace属性
  26. QName opName = new QName("http://test.webservice", "plus");
  27. // 执行方法获取返回值
  28. // 没有返回值的方法使用serviceClient.invokeRobust(opName, entryArgs)
  29. Object result = serviceClient.invokeBlocking(opName, entryArgs, classes)[0];
  30. System.out.println(result);
  31. // out: 3.0
  32. }
  33. }

以上是实现了一个简单的WebSerivce客户端,调用CalculateService中的plus方法,由代码可见,这种调用方式比较杂乱,代码不太友好。

wsdl2java简化客户端

<Axis2客户端安装目录>/bin目录,其内有两个bat,wsdl2java.bat和java2wsdl.bat,可以实现WSDL文件和Java之间的互相转换。

考虑到我们以后可能经常使用这些命令,设置环境变量,方便以后调用。在系统变量中加入AXIS2_HOME=<Axis2客户端安装目录>,path中追加;%AXIS2_HOME%\bin。

启动命令提示符,进入WebServiceTestClient所在目录,运行

  1. wsdl2java -uri http://localhost:8080/axis2/services/CalculateService?wsdl -p webservice.client.test -s

参数说明:uri - wsdl文件路径,网络路径或本地路径,p - 打包,这里和上一个客户端实现类打在了一个包里,wsdl2java有很多参数,详细可以运行该命令去查看。

执行后,如果没有报错,说明运行成功,刷新项目,该包下多出了一个CalculateServiceStub类,里面的代码极其复杂,还乱呼呼的,这我们不用管,调用该类。

  1. package webservice.client.test;
  2. import java.rmi.RemoteException;
  3. import webservice.client.test.CalculateServiceStub.Plus;
  4. public class Client2 {
  5. /**
  6. * @param args
  7. * @throws RemoteException
  8. */
  9. public static void main(String[] args) throws RemoteException {
  10. CalculateServiceStub stub = new CalculateServiceStub();
  11. Plus plus = new Plus();
  12. plus.setX(1);
  13. plus.setY(2);
  14. float result = stub.plus(plus).get_return();// 返回值自动转型,这也是强大之处
  15. System.out.println(result);
  16. }
  17. }

如此做的好处就是调用时不需要在去查看WSDL,和正常使用一个类一样,对WebService的封装都由wsdl2java自动生成,代码更优雅、简洁。

利用wsdl2java轻松使用第三方WebService服务

有了wsdl2java,已知一个WSDL文件我们就可以轻松的生成WebService客户端供我们调用,给我们服务。文章开头给出的链接包含了一些第三方服务,有一个服务是生成随机个数中文,WSDL:http://www.webxml.com.cn/WebServices/RandomFontsWebService.asmx?wsdl,同样,启动命令提示符,进入项目路径,执行

  1. wsdl2java -uri http://www.webxml.com.cn/WebServices/RandomFontsWebService.asmx?wsdl -p webservice.client.test -s

调用该类

  1. package webservice.client.test;
  2. import java.rmi.RemoteException;
  3. import webservice.client.test.RandomFontsWebServiceStub.ArrayOfString;
  4. import webservice.client.test.RandomFontsWebServiceStub.GetChineseFonts;
  5. public class ThirdClient {
  6. /**
  7. * @param args
  8. * @throws RemoteException
  9. */
  10. public static void main(String[] args) throws RemoteException {
  11. RandomFontsWebServiceStub stub = new RandomFontsWebServiceStub();
  12. GetChineseFonts getChineseFonts = new GetChineseFonts();
  13. getChineseFonts.setByFontsLength(10);// 免费使用有限制,最多8个
  14. ArrayOfString result = stub.getChineseFonts(getChineseFonts).getGetChineseFontsResult();
  15. for(String str : result.getString()) {
  16. System.out.println(str);
  17. }
  18. }
  19. }

源码下载

       文中代码尽在下面链接中,免积分下载。

http://download.csdn.net/download/ghsau/6400843

(完)

本文来自:高爽|Coder,原文地址:http://blog.csdn.net/ghsau/article/details/12714965

最新文章

  1. 基于Metronic的Bootstrap开发框架总览
  2. EF架构~EF6配置需要注意的几个地方
  3. 矩阵乘法 --- hdu 4920 : Matrix multiplication
  4. discuz插件开发新手入门 超详细
  5. iOS XMPP Framework 中文概述
  6. linux下搭建nginx+php(FastCGI)+mysql运行环境
  7. google map getLocation参考
  8. UVALive3713-Astronauts(2-SAT)
  9. [Leetcode] Remove Duplicates From Sorted Array II (C++)
  10. centos7进单用户
  11. cron表达式学习
  12. linux相关操作命令
  13. Yii2 组件
  14. UI自动化(十)selenium定位
  15. 栈(stack)和堆(heap)
  16. 常用for循环和for in 以及for of 的区别
  17. Unity中Button按钮的触发监听事件
  18. SharePoint Framework 把你的客户端web部件连接到SharePoint
  19. sdn2017 第三次作业
  20. 解题:SHOI 2006 有色图

热门文章

  1. Tomcat在eclipse中起动成功,主页却打不开
  2. (C++) Interview in English. - Constructors/Destructors
  3. SIGPIPE
  4. Spark RDDRelation
  5. SOA_环境安装系列1_Oracle SOA Suite11g安装总括(案例)
  6. bug_ _小心android-support-v4.jar版本混乱造成的NoClassDefFoundError
  7. ibatis插入数据后返回自增长的主键
  8. 下载文件的一种简单方法js
  9. htmlparser日记
  10. c# as