加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Javawebservice例子

发布时间:2020-12-16 23:46:16 所属栏目:安全 来源:网络整理
导读:1.参考文献: 1.利用Java编写简单的WebService实例 ?http://nopainnogain.iteye.com/blog/791525 2.Axis2与Eclipse整合开发Web Service ?http://tech.ddvip.com/2009-05/1242968642120461.html 3.http://blog.csdn.net/lightao220/article/details/3489015 4

1.参考文献:

1.利用Java编写简单的WebService实例 ?http://nopainnogain.iteye.com/blog/791525

2.Axis2与Eclipse整合开发Web Service ?http://tech.ddvip.com/2009-05/1242968642120461.html

3.http://blog.csdn.net/lightao220/article/details/3489015

4.http://clq9761.iteye.com/blog/976029

5.使用Eclipse+Axis2+Tomcat构建Web Services应用(实例讲解篇)

2.实例1(主要看到[2])

2.1.系统功能:?

开发一个计算器服务CalculateService,这个服务包含加(plus)、减(minus)、乘(multiply)、除(divide)的操作。

2.2.开发前准备:

  1. 安装Eclipse-jee;
  2. 下载最新版本的Axis2,网址http://axis.apache.org/axis2/java/core/download.cgi?,选择Standard Binary Distribution的zip包,解压缩得到的目录名axis2-1.4.1,目录内的文件结构如下:


2.3.开发前配置:

在Eclipse的菜单栏中,Window --> Preferences --> Web Service --> Axis2?Perferences,在Axis2 runtime location中选择Axis2解压缩包的位置,设置好后,点"OK"即行。(如图


2.4.开发Web Service:

(1)新建一个Java Project,命名为"WebServiceTest1"
(2)新建一个class,命名为"CalculateService",完整代码如下:

[java]? view plain copy
  1. package?edu.sjtu.webservice;??
  2. /**?
  3. ?*?计算器运算?
  4. ?*?@author?rongxinhua?
  5. ?*/??
  6. public?class?CalculateService?{??
  7. ????//加法??
  8. ????float?plus(float?x,?float?y)?{??
  9. ????????return?x?+?y;??
  10. ????}??
  11. //减法??
  12. float?minus(return?x?-?y;??
  13. //乘法??
  14. float?multiply(return?x?*?y;??
  15. //除法??
  16. float?divide(if(y!=0)??
  17. ????????{??
  18. ????????????return?x?/?y;??
  19. ????????}??
  20. else??
  21. ????????????return?-1;??
  22. ????}??
  23. }??
(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),如下图:


(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服务器了。

(9)等启完后,点击"next -- > next",一切默认即行,最后,点击完成。最后,出现如下界面:(Web?Service Explorer),我们在这里便可测试我们的Web服务。(使用浏览器打开的话使用如下地址:http://127.0.0.1:19189/wse/wsexplorer/wsexplorer.jsp?org.eclipse.wst.ws.explorer=3)。如下图所示:


注:在浏览器中打开Web Service Explorer(有时候在eclipse中关闭了webservice explorer,可以用这种方法打开)

首先登录地址:http://127.0.0.1:19189/wse/wsexplorer/wsexplorer.jsp。然后在网页右上角选择Web Service Exoplorer标签。然后输入WSDL地址:http://localhost:8080/WebServiceTest1/services/CalculateService?wsdl 。这个wsdl地址就是我们刚才发布服务的那个wsdl。点击go,如下图所示:


然后就可以看到如下界面了:


(10)测试比较简单,例如,我们选择一个"plus"的Operation(必须是CalculateServiceSoap11Binding),出现下图,在x的输入框中输入1,在y的输入框中输入2,点击"go",便会在status栏中显示结果3.0。其他方法的测试也类似。结果如上图所示。

2.5.CalculateService客户端调用程序

前面我们已经定义好了加减乘除的方法并将这些方法发布为服务,那么现在要做的就是调用这些服务即可。客户端调用程序如下代码所示:CalculateServiceTest.java
[java]? view plain copy
    package?edu.sjtu.webservice.test;??
  1. ??
  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. class?CalculateServiceTest?{??
  8. /**?
  9. ?????*?@param?args?
  10. ?????*?@throws?AxisFault?
  11. ?????*/??
  12. ????static?void?main(String[]?args)?throws?AxisFault?{??
  13. ????????//?TODO?Auto-generated?method?stub??
  14. ??
  15. //?使用RPC方式调用WebService??
  16. ????????RPCServiceClient?serviceClient?=?new?RPCServiceClient();??
  17. ????????Options?options?=?serviceClient.getOptions();??
  18. ????????//?指定调用WebService的URL??
  19. ????????EndpointReference?targetEPR?=?new?EndpointReference(??
  20. ????????????????"http://localhost:8080/WebServiceTest1/services/CalculateService");??
  21. ????????options.setTo(targetEPR);??
  22. //?指定要调用的计算机器中的方法及WSDL文件的命名空间:edu.sjtu.webservice。??
  23. ????????QName?opAddEntry?=?new?QName("http://webservice.sjtu.edu","plus");//加法??
  24. ????????QName?opAddEntryminus?=?"minus");//减法??
  25. ????????QName?opAddEntrymultiply?=?"multiply");//乘法??
  26. ????????QName?opAddEntrydivide?=?"divide");//除法??
  27. //?指定plus方法的参数值为两个,分别是加数和被加数??
  28. ????????Object[]?opAddEntryArgs?=?new?Object[]?{?1,2?};??
  29. //?指定plus方法返回值的数据类型的Class对象??
  30. ????????Class[]?classes?=?new?Class[]?{?float.class?};??
  31. //?调用plus方法并输出该方法的返回值??
  32. ????????System.out.println(serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs,?classes)[0]);??
  33. ????????System.out.println(serviceClient.invokeBlocking(opAddEntryminus,0); background-color:inherit">0]);??
  34. ????????System.out.println(serviceClient.invokeBlocking(opAddEntrymultiply,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> ????????System.out.println(serviceClient.invokeBlocking(opAddEntrydivide,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> ????}??
  35. }??
运行结果:
copy
    3.0??
  1. -1.0??
  2. 2.0??
  3. 0.5??

3.实例2.HelloService

(1)首先定义服务方法,代码如下所示:

copy
    ??
  1. class?HelloService?{??
  2. public?String?sayHelloNew()?{??
  3. return?"hello";??
  4. ??
  5. public?String?sayHelloToPersonNew(String?name)?{??
  6. if?(name?==?null)?{??
  7. ????????????name?=?"nobody";??
  8. ????????}??
  9. ????????return?"hello,"?+?name;??
  10. void?updateData(String?data)?{??
  11. ????????System.out.println(data?+?"?已更新。");??
  12. (2)参考实例1将这个方法发布为服务。

    (3)编写客户端代码调用WebService(主要参考[5])

    本文例子与其他例子最大的不同就在这里,其他例子一般需要根据刚才的服务wsdl生成客户端stub,然后通过stub来调用服务,这种方式显得比较单一,客户端必须需要stub存根才能够访问服务,很不方面。本例子的客户端不采用stub方式,而是一种实现通用的调用方式,不需要任何客户端存根即可访问服务。只需要指定对于的web servce地址、操作名、参数和函数返回类型即可。代码如下所示:

    HelloServiceTest2.java

    copy
      package?edu.sjtu.webservice.test;??
    1. import?javax.xml.namespace.QName;??
    2. import?org.apache.axis2.AxisFault;??
    3. import?org.apache.axis2.addressing.EndpointReference;??
    4. import?org.apache.axis2.client.Options;??
    5. import?org.apache.axis2.rpc.client.RPCServiceClient;??
    6. class?HelloServiceTest2?{??
    7. private?RPCServiceClient?serviceClient;??
    8. private?Options?options;??
    9. private?EndpointReference?targetEPR;??
    10. public?HelloServiceTest2(String?endpoint)?throws?AxisFault?{??
    11. ????????serviceClient?=?new?RPCServiceClient();??
    12. ????????options?=?serviceClient.getOptions();??
    13. ????????targetEPR?=?new?EndpointReference(endpoint);??
    14. ????????options.setTo(targetEPR);??
    15. public?Object[]?invokeOp(String?targetNamespace,?String?opName,??
    16. ????????????Object[]?opArgs,?Class<?>[]?opReturnType)?throws?AxisFault,??
    17. ????????????ClassNotFoundException?{??
    18. //?设定操作的名称??
    19. ????????QName?opQName?=?new?QName(targetNamespace,?opName);??
    20. //?设定返回值??
    21. //?Class<?>[]?opReturn?=?new?Class[]?{?opReturnType?};??
    22. //?操作需要传入的参数已经在参数中给定,这里直接传入方法中调用??
    23. return?serviceClient.invokeBlocking(opQName,?opArgs,?opReturnType);??
    24. /**?
    25. ?????*?@param?args?
    26. ?????*?@throws?AxisFault?
    27. ?????*?@throws?ClassNotFoundException?
    28. ?????*/??
    29. ????????????ClassNotFoundException?{??
    30. //?TODO?Auto-generated?method?stub??
    31. final?String?endPointReference?=?"http://localhost:8080/WebServiceTest1/services/HelloService";??
    32. final?String?targetNamespace?=?"http://webservice.sjtu.edu";??
    33. ????????HelloServiceTest2?client?=?new?HelloServiceTest2(endPointReference);??
    34. ????????String?opName?=?"sayHelloToPersonNew";??
    35. ????????Object[]?opArgs?=?new?Object[]?{?"My?Friends"?};??
    36. ????????Class<?>[]?opReturnType?=?new?Class[]?{?String[].class?};??
    37. ????????Object[]?response?=?client.invokeOp(targetNamespace,?opName,??
    38. ????????????????opReturnType);??
    39. ????????System.out.println(((String[])?response[0])[0]);??
    40. 运行该程序,点击Run As->Java application,可以看到控制台端口的输出是:Hello,My Friends,表明客户端调用成功。该例子最大的不同和优势表现在客户端的调用方式,或者说是发起服务调用的方式,虽然比起客户端stub存根的方式,代码稍多,但是这种方式统一,不需要生产stub存根代码,解决了客户端有很多类的问题。如果读者对这些代码进一步封装,我想调用方式很简单,只需要传递相关参数,这更好地说明了服务调用的优势。而且这种方式更加简单明了,一看便知具体含义。而不需要弄得stub类的一些机制。

      (4)改写客户端调用服务的代码

      (3)中提到的客户端应用代码写的略微有些繁杂,下面将上面的客户端调用service程序进行改写,简洁了许多。代码如下:

      HelloServiceTest.java

      copy

        import?org.apache.axis2.AxisFault;??
      1. import?org.apache.axis2.addressing.EndpointReference;??
      2. import?org.apache.axis2.client.Options;??
      3. import?org.apache.axis2.rpc.client.RPCServiceClient;??
      4. class?HelloServiceTest?{??
      5. void?main(String?args[])?throws?AxisFault?{??
      6. //?使用RPC方式调用WebService??
      7. ????????RPCServiceClient?serviceClient?=? ????????Options?options?=?serviceClient.getOptions();??
      8. //?指定调用WebService的URL??
      9. ????????EndpointReference?targetEPR?=?new?EndpointReference("http://localhost:8080/WebServiceTest1/services/HelloService");??
      10. ????????options.setTo(targetEPR);??
      11. ??????????
      12. //?指定要调用的sayHelloToPerson方法及WSDL文件的命名空间??
      13. "sayHelloToPersonNew");??
      14. //?指定sayHelloToPerson方法的参数值??
      15. ????????Object[]?opAddEntryArgs?=?new?Object[]?{?"xuwei"?};??
      16. //?指定sayHelloToPerson方法返回值的数据类型的Class对象??
      17. ????????Class[]?classes?=?new?Class[]?{?String.//?调用sayHelloToPerson方法并输出该方法的返回值??
      18. ????????System.out.println(serviceClient.invokeBlocking(opAddEntry,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> }??

      (编辑:李大同)

      【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读