天天看點

webservice 開發 axis2 簡單部署服務

axis2-1.4.1 下的

axis2.war 放到tomcat下

簡單編寫一個服務,供給系統外部調用

Java代碼

webservice 開發 axis2 簡單部署服務
webservice 開發 axis2 簡單部署服務
webservice 開發 axis2 簡單部署服務

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



public class SimpleService {

	public String getGreeting(String name) {
		return "你好 "+name;
	}
	
	public int getPrice() {
		return new Random().nextInt(1000);
	}
}
      
webservice 開發 axis2 簡單部署服務
webservice 開發 axis2 簡單部署服務
webservice 開發 axis2 簡單部署服務
  1. package client;
  2. import java.rmi.RemoteException;
  3. import java.util.Iterator;
  4. import javax.xml.namespace.QName;
  5. import org.apache.axiom.om.OMAbstractFactory;
  6. import org.apache.axiom.om.OMElement;
  7. import org.apache.axiom.om.OMFactory;
  8. import org.apache.axiom.om.OMNamespace;
  9. import org.apache.axis2.AxisFault;
  10. import org.apache.axis2.Constants;
  11. import org.apache.axis2.addressing.EndpointReference;
  12. import org.apache.axis2.client.Options;
  13. import org.apache.axis2.client.ServiceClient;
  14. import org.apache.axis2.rpc.client.RPCServiceClient;
  15. public class RPCClient {
  16. public static void main(String[] args) throws RemoteException {
  17. //RPC 方式調用 服務端
  18. //runRPC();
  19. //Axiom 方式調用 服務端
  20. //runAxiom();
  21. //wsdl2java.bat -uri http://localhost:8080/axis2/services/SimpleService?wsdl -p client -s -o stub
  22. //工具自動生成
  23. SimpleServiceStub stub = new SimpleServiceStub();
  24. SimpleServiceStub.GetGreeting gg = new SimpleServiceStub.GetGreeting();
  25. gg.setName("超人");
  26. System.out.println(stub.getGreeting(gg).get_return());
  27. System.out.println(stub.getPrice().get_return());
  28. /**
  29. * RPC方式調用 服務
  30. * <功能較長的描述>
  31. * @throws AxisFault
  32. * @throws Exception
  33. * @see [類、類#方法、類#成員]
  34. */
  35. public static void runRPC() throws AxisFault {
  36. // 使用 RPC方式調用 webservcie
  37. RPCServiceClient serviceClient = new RPCServiceClient();
  38. Options options = serviceClient.getOptions();
  39. // 指定調用 WebService 的URTL
  40. EndpointReference taretEPR = new EndpointReference(
  41. "http://localhost:8080/axis2/services/SimpleService");
  42. options.setTo(taretEPR);
  43. //指定 getGreeting方法的參數值
  44. Object[] opAddEntryArgs = new Object[]{"超人"};
  45. //指定 getGreeting方法的傳回值的資料類型的class對象
  46. Class[] classes = new Class[]{String.class};
  47. //指定 要調用的 getGreeting方法及WSDL檔案的命名空間
  48. QName opAddEntry = new QName("http://ws.apache.org/axis2","getGreeting");
  49. //調用getGreeting方法并輸出該方法的傳回值
  50. System.out.println(serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs,classes)[0]);
  51. classes = new Class[]{int.class};
  52. opAddEntry = new QName("http://ws.apache.org/axis2","getPrice");
  53. }   

繼續閱讀