Three ways to call Axis2 WebService

Source: Internet
Author: User
Tags gettext wsdl

[Java]View Plaincopy
  1. Package testClick.src.test;
  2. Import Javax.xml.namespace.QName;
  3. Import Org.apache.axiom.om.OMAbstractFactory;
  4. Import org.apache.axiom.om.OMElement;
  5. Import Org.apache.axiom.om.OMFactory;
  6. Import Org.apache.axiom.om.OMNamespace;
  7. Import Org.apache.axis2.AxisFault;
  8. Import org.apache.axis2.addressing.EndpointReference;
  9. Import org.apache.axis2.client.Options;
  10. Import org.apache.axis2.client.ServiceClient;
  11. Import org.apache.axis2.rpc.client.RPCServiceClient;
  12. Import Org.junit.Test;
  13. Import TestClick.src.testclick.HelloWebService;
  14. Import TestClick.src.testclick.HelloWebServicePortType;
  15. Public class TestWebService {
  16. @Test
  17. public void Test () {
  18. //testclient ();
  19. //testrpc ();
  20. Testomelement ();
  21. }
  22. /** 
  23. * First Type
  24. * MyEclipse Generate client Mode
  25. */
  26. public void TestClient () {
  27. Hellowebservice service = new Hellowebservice ();
  28. Hellowebserviceporttype PortType = Service.gethellowebservicehttpsoap11endpoint ();
  29. String result = Porttype.sayhello ("Jack");
  30. SYSTEM.OUT.PRINTLN (result);
  31. }
  32. /** 
  33. * Second Type
  34. * Use AXIS2 RPC call WebService to import the appropriate jar package
  35. */
  36. public void Testrpc () {
  37. try{
  38. Rpcserviceclient client = new Rpcserviceclient ();
  39. Options options = Client.getoptions ();
  40. String url = "http://localhost:8080/axis2/services/HelloWebService?wsdl";
  41. EndpointReference end = new EndpointReference (URL);
  42. Options.setto (end);
  43. object[] obj = new object[]{"Tom"};
  44. class<?>[] classes = new class[] {String.     Class};
  45. QName QName = new QName ("Http://testClick", "SayHello");
  46. String result = (string) client.invokeblocking (QName, obj,classes) [0];
  47. SYSTEM.OUT.PRINTLN (result);
  48. }catch (Axisfault e) {
  49. E.printstacktrace ();
  50. }
  51. }
  52. /** 
  53. * Third Type
  54. * @throws Axisfault
  55. */
  56. public void Testomelement () {
  57. try{
  58. ServiceClient sc = new serviceclient ();
  59. Options opts = new options ();
  60. String url = "http://localhost:8080/axis2/services/HelloWebService?wsdl";
  61. EndpointReference end = new EndpointReference (URL);
  62. Opts.setto (end);
  63. Opts.setaction ("SayHello");
  64. Sc.setoptions (opts);
  65. Omfactory FAC = omabstractfactory.getomfactory ();
  66. Omnamespace omns = Fac.createomnamespace ("Http://testClick", "" ");
  67. Omelement method = Fac.createomelement ("SayHello", omns);
  68. Omelement value = fac.createomelement ("name", Omns);
  69. Value.settext ("admin");
  70. Method.addchild (value);
  71. omelement res = sc.sendreceive (method);
  72. Res.getfirstelement (). GetText ();
  73. System.out.println (Res.getfirstelement (). GetText ());
  74. }catch (Axisfault e) {
  75. E.printstacktrace ();
  76. }
  77. }
  78. }

Three ways to call Axis2 WebService

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.