Eclipse+axis2+webservice Development Examples

Source: Internet
Author: User
Tags wsdl

1. Reference documents:

1. Write a simple WebService instance using Java http://nopainnogain.iteye.com/blog/791525

2.axis2 integrates with Eclipse to develop 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. Building a Web Services application using Eclipse+axis2+tomcat (example tutorial)

2. Example 1 (see [2]) 2.1. System function: Develop a calculator service Calculateservice, this service contains plus (plus), minus (minus), multiply (multiply), except (divide) operation.

2.2. Preparation before development:

    1. Install Eclipse-jee;
    2. Download the latest version of Axis2, URL http://axis.apache.org/axis2/java/core/download.cgi, select the standard Binary distribution Zip package, Extract the resulting directory name axis2-1.4.1, the file structure in the directory is as follows:

2.3. Pre-development configuration: In the Eclipse menu bar, Window-----Preferences Web Service--Axis2 perferences, AXIS2 runtime Select the location of the Axis2 decompression package, set up, click "OK" to the right. ()

2.4. Developing Web Service:

(1) Create a new Java Project named "WebServiceTest1"
(2) Create a new class named "Calculateservice", complete with the following code:

[Java]View Plaincopy
  1. Package edu.sjtu.webservice;
  2. /**
  3. * Calculator operation
  4. * @author Rongxinhua
  5. */
  6. public class Calculateservice {
  7. Addition
  8. public float plus (float x, float y) {
  9. return x + y;
  10. }
  11. Subtraction
  12. public float minus (float x, float y) {
  13. return x-y;
  14. }
  15. Multiplication
  16. public float multiply (float x, float y) {
  17. return x * y;
  18. }
  19. Division
  20. public float divide (float x, float y) {
  21. if (y!=0)
  22. {
  23. return x/y;
  24. }
  25. Else
  26. return-1;
  27. }
  28. }

(3) on the "WebServiceTest1" project, New----and other, find "Web Service" under "Web Services";

(4) Next, in the Web Services object box that appears, click "Browse" in the service implementation to go to the Browse classes object frame, Find the Calculateservice class that we just wrote. (e.g.). Click "OK" to return to the Web Service session box.

(5) In the Web Service dialog box, move the slider in the Web service type to the location of start Service and the slider in client type to the "Test client" location.

(6) On the right side of the Web Service type slider, there is a "Configuration", click on the option below it and enter the Service Deployment Configuration Object box, where you select the appropriate server ( I use Tomcat6.0) and Web Service runtime (choose Apache Axis2), such as:

(7) Click OK, then return to the Web Service dialog box, the same way, the Client type in the right side of the slider also has "Configuration", but also to the corresponding set, the same step. When you are done, next----next. Into the Axis2 Web Service Java Bean Configuration, we select Generate a default Services.xml, as shown in:

(8) To the Server Startup dialog box, there is a button "Start server" (such as), click on it, you can start the Tomcat server.

(9) After the start, click "Next-> Next", all by default, and finally, click Done. Finally, the following interface appears: (Web Service Explorer), where we can test our web services. (Use the following address when opening a browser: http://127.0.0.1:19189/wse/wsexplorer/wsexplorer.jsp?org.eclipse.wst.ws.explorer=3). As shown in the following:

Note: Open the Web Service Explorer in the browser (sometimes WebService Explorer is turned off in eclipse, you can open it in this way)

First login address: http://127.0.0.1:19189/wse/wsexplorer/wsexplorer.jsp. Then select the Web Service exoplorer tag in the top right corner of the webpage. Then enter the WSDL address: http://localhost:8080/WebServiceTest1/services/CalculateService?wsdl. This WSDL address is the WSDL of the service we just published. Click Go, as shown in:

You can then see the following interface:

(10) test is relatively simple, for example, we select a "plus" operation (must be calculateservicesoap11binding), appear, enter in the X Input box 1, enter the Y input box 2, click "Go , the result 3.0 is displayed in the status bar. Other methods of testing are similar. The results are as shown.

2.5.CalculateService client callers before we have defined the subtraction method and published these methods as services, all we have to do now is invoke these services. The client invoker is shown in the following code: Calculateservicetest.java [Java]View Plaincopy
  1. Package edu.sjtu.webservice.test;
  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. public class Calculateservicetest {
  8. /**
  9. * @param args
  10. * @throws Axisfault
  11. */
  12. public static void Main (string[] args) throws Axisfault {
  13. TODO auto-generated Method Stub
  14. Call WebService using RPC mode
  15. Rpcserviceclient serviceclient = new Rpcserviceclient ();
  16. Options options = Serviceclient.getoptions ();
  17. Specifies the URL to call WebService
  18. EndpointReference Targetepr = new EndpointReference (
  19. "Http://localhost:8080/WebServiceTest1/services/CalculateService");
  20. Options.setto (TARGETEPR);
  21. Specifies the method in the computer to invoke and the namespace of the WSDL file: Edu.sjtu.webservice.
  22. QName opaddentry = new QName ("http://webservice.sjtu.edu", "plus");//addition
  23. QName Opaddentryminus = new QName ("http://webservice.sjtu.edu", "minus");//Subtraction
  24. QName opaddentrymultiply = new QName ("http://webservice.sjtu.edu", "multiply");//multiplication
  25. QName opaddentrydivide = new QName ("http://webservice.sjtu.edu", "divide");//Division
  26. The parameter value for the specified plus method is two, Addend and Summand, respectively.
  27. object[] Opaddentryargs = new object[] {n};
  28. Class object specifying the data type of the plus method return value
  29. Class[] classes = new class[] {float.class};
  30. Call the plus method and output the return value of the method
  31. System.out.println (Serviceclient.invokeblocking (Opaddentry,opaddentryargs, classes) [0]);
  32. System.out.println (Serviceclient.invokeblocking (Opaddentryminus,opaddentryargs, classes) [0]);
  33. System.out.println (Serviceclient.invokeblocking (Opaddentrymultiply,opaddentryargs, classes) [0]);
  34. System.out.println (Serviceclient.invokeblocking (Opaddentrydivide,opaddentryargs, classes) [0]);
  35. }
  36. }
Operation Result: [Java]View Plaincopy
    1. 3.0
    2. -1.0
    3. 2.0
    4. 0.5
3. Example 2. HelloService

(1) First define the service method, the code is as follows:

[Java]View Plaincopy
  1. Package edu.sjtu.webservice;
  2. public class HelloService {
  3. Public String sayhellonew () {
  4. return "Hello";
  5. }
  6. public string Sayhellotopersonnew (string name) {
  7. if (name = = null) {
  8. Name = "Nobody";
  9. }
  10. Return "Hello," + name;
  11. }
  12. public void UpdateData (String data) {
  13. SYSTEM.OUT.PRINTLN (Data + "has been updated.) ");
  14. }
  15. }

(2) Refer to Example 1 to publish this method as a service.

(3) Writing client code call WebService (main reference [5])

The most important difference between this example and other examples is that here, other examples generally need to generate a client stub based on the service WSDL just now, and then invoke the service through the stub, which seems to be relatively single, and the client must need stub stubs to be able to access the service, very little. The client in this example does not use stubs, but rather a generic invocation that does not require any client stubs to access the service. You only need to specify the Web servce address, operation name, parameter, and function return type for. The code looks like this:

Helloservicetest2.java [Java]View Plaincopy
  1. Package edu.sjtu.webservice.test;
  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. public class HelloServiceTest2 {
  8. Private Rpcserviceclient serviceclient;
  9. private options options;
  10. Private EndpointReference Targetepr;
  11. Public HelloServiceTest2 (String endpoint) throws Axisfault {
  12. ServiceClient = new Rpcserviceclient ();
  13. options = Serviceclient.getoptions ();
  14. Targetepr = new EndpointReference (endpoint);
  15. Options.setto (TARGETEPR);
  16. }
  17. Public object[] Invokeop (string targetnamespace, String opname,
  18. Object[] Opargs, class<?>[] opreturntype) throws Axisfault,
  19. classnotfoundexception {
  20. Set the name of the action
  21. QName opqname = new QName (targetnamespace, opname);
  22. Set the return value
  23. class<?>[] Opreturn = new class[] {opreturntype};
  24. The operation needs to pass in parameters that have been given in the parameters, which are called directly into the method.
  25. Return serviceclient.invokeblocking (Opqname, Opargs, Opreturntype);
  26. }
  27. /**
  28. * @param args
  29. * @throws Axisfault
  30. * @throws ClassNotFoundException
  31. */
  32. public static void Main (string[] args) throws Axisfault,
  33. classnotfoundexception {
  34. TODO auto-generated Method Stub
  35. Final String endpointreference = "Http://localhost:8080/WebServiceTest1/services/HelloService";
  36. Final String targetnamespace = "http://webservice.sjtu.edu";
  37. HelloServiceTest2 client = new HelloServiceTest2 (endpointreference);
  38. String opname = "Sayhellotopersonnew";
  39. object[] Opargs = new object[] {"My Friends"};
  40. class<?>[] Opreturntype = new class[] {string[].class};
  41. object[] Response = Client.invokeop (targetnamespace, opname, Opargs,
  42. Opreturntype);
  43. System.out.println (((string[) response[0]) [0]);
  44. }
  45. }

Run the program, click Run As->java application, and you can see that the output of the console port is: Hello, My Friends, indicating that the client call was successful. The biggest differences and advantages of this example are expressed in the way the client calls, or in the manner of initiating a service invocation, although there is a bit more code than the client stub stub, but this is uniform and does not require the production of stub stub code, which solves the problem of many classes for the client. If the reader encapsulates the code further, I would like to call it simply by passing the relevant parameters, which will better illustrate the benefits of service invocation. And this way is more simple and clear, one can see the specific meaning. Without having to make some mechanism of the stub class.

(4) Rewrite code for client invoke service

(3) mentioned in the client application code is slightly cumbersome to write, the following client call service program to rewrite, a lot more concise. The code is as follows:

Helloservicetest.java

[Java]View Plaincopy
    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. public class Helloservicetest {
    7. public static void Main (String args[]) throws Axisfault {
    8. Call WebService using RPC mode
    9. Rpcserviceclient serviceclient = new Rpcserviceclient ();
    10. Options options = Serviceclient.getoptions ();
    11. Specifies the URL to call WebService
    12. EndpointReference Targetepr = new EndpointReference ("Http://localhost:8080/WebServiceTest1/services/HelloService") ;
    13. Options.setto (TARGETEPR);
    14. Specify the Sayhellotoperson method to invoke and the namespace of the WSDL file
    15. QName opaddentry = new QName ("http://webservice.sjtu.edu", "sayhellotopersonnew");
    16. Specifying the parameter value of the Sayhellotoperson method
    17. object[] Opaddentryargs = new object[] {"Xuwei"};
    18. Class object specifying the data type of the Sayhellotoperson method return value
    19. Class[] classes = new class[] {string.class};
    20. Call the Sayhellotoperson method and output The return value of the method
    21. System.out.println (Serviceclient.invokeblocking (Opaddentry,opaddentryargs, classes) [0]);
    22. }
    23. }

Eclipse+axis2+webservice Development Examples

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.