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

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

Java代码 webservice 开发 axis2 简单部署服务 _服务 webservice 开发 axis2 简单部署服务 _开发_02
  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代码 webservice 开发 axis2 简单部署服务 _服务 webservice 开发 axis2 简单部署服务 _开发_02
  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, opAddEntryArgs,classes)[0]);
  68. }