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

webservice 开发 axis2 简单部署服务

发布时间:2020-12-17 00:26:36 所属栏目:安全 来源:网络整理
导读:axis2-1.4.1 下的 axis2.war 放到tomcat下 简单编写一个服务,供给系统外部调用 Java代码 import java.util.Random; ? ? ? public class SimpleService { ? public String getGreeting(String name) { return "你好 " +name; } ? public int getPrice() { re

axis2-1.4.1 下的
axis2.war 放到tomcat下

简单编写一个服务,供给系统外部调用

Java代码

  1. import java.util.Random;
  2. ?
  3. ?
  4. ?
  5. public class SimpleService {
  6. ?
  7. public String getGreeting(String name) {
  8. return "你好 "+name;
  9. }
  10. ?
  11. public int getPrice() {
  12. return new Random().nextInt(1000);
  13. }
  14. }



SimpleService.class 放到 WEB-INF/pojo 下
就这么简单 就构成了一个服务

客户端(Java):

?

Java代码

  1. package client;
  2. ?
  3. import java.rmi.RemoteException;
  4. import java.util.Iterator;
  5. ?
  6. import javax.xml.namespace.QName;
  7. ?
  8. import org.apache.axiom.om.OMAbstractFactory;
  9. import org.apache.axiom.om.OMElement;
  10. import org.apache.axiom.om.OMFactory;
  11. import org.apache.axiom.om.OMNamespace;
  12. import org.apache.axis2.AxisFault;
  13. import org.apache.axis2.Constants;
  14. import org.apache.axis2.addressing.EndpointReference;
  15. import org.apache.axis2.client.Options;
  16. import org.apache.axis2.client.ServiceClient;
  17. import org.apache.axis2.rpc.client.RPCServiceClient;
  18. ?
  19. public class RPCClient {
  20. ?
  21. public static void main(String[] args) throws RemoteException {
  22. ?
  23. //RPC 方式调用 服务端
  24. //runRPC();
  25. ?
  26. //Axiom 方式调用 服务端
  27. //runAxiom();
  28. ?
  29. //wsdl2java.bat -uri http://localhost:8080/axis2/services/SimpleService?wsdl -p client -s -o stub
  30. //工具自动生成
  31. SimpleServiceStub stub = new SimpleServiceStub();
  32. SimpleServiceStub.GetGreeting gg = new SimpleServiceStub.GetGreeting();
  33. gg.setName("超人");
  34. System.out.println(stub.getGreeting(gg).get_return());
  35. System.out.println(stub.getPrice().get_return());
  36. }
  37. ?
  38. /**
  39. * RPC方式调用 服务
  40. * <功能详细描述>
  41. * @throws AxisFault
  42. * @throws Exception
  43. * @see [类、类#方法、类#成员]
  44. */
  45. public static void runRPC() throws AxisFault {
  46. // 使用 RPC方式调用 webservcie
  47. RPCServiceClient serviceClient = new RPCServiceClient();
  48. Options options = serviceClient.getOptions();
  49. ?
  50. // 指定调用 WebService 的URTL
  51. EndpointReference taretEPR = new EndpointReference(
  52. "http://localhost:8080/axis2/services/SimpleService");
  53. options.setTo(taretEPR);
  54. ?
  55. //指定 getGreeting方法的参数值
  56. Object[] opAddEntryArgs = new Object[]{"超人"};
  57. //指定 getGreeting方法的返回值的数据类型的class对象
  58. Class[] classes = new Class[]{String.class};
  59. //指定 要调用的 getGreeting方法及WSDL文件的命名空间
  60. QName opAddEntry = new QName("http://ws.apache.org/axis2","getGreeting");
  61. ?
  62. //调用getGreeting方法并输出该方法的返回值
  63. System.out.println(serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs,classes)[0]);
  64. ?
  65. classes = new Class[]{int.class};
  66. opAddEntry = new QName("http://ws.apache.org/axis2","getPrice");
  67. System.out.println(serviceClient.invokeBlocking(opAddEntry,classes)[0]);
  68. } ??

(编辑:李大同)

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

    推荐文章
      热点阅读