Use eclipse + axis2 to build a web service application (the client does not need to generate the stub method)

Source: Internet
Author: User

I. Development Environment and Software Download

The development environment is eclipse3.4 + jdk1.6 + tomcat6.0 + axis2 1.3. These software are all the latest versions of each software. The download method is updated by the software website and the address is also updated. The download method will not be posted here. Please download the software from the corresponding website.
Software Download and installation:
(1) jdk1.6 environment variable Configuration:
Java_home: C: \ Program Files \ Java \ jdk1.6.0 _ 07, where c: \ Program Files \ Java is my JDK installation directory.
Path: % java_home % \ bin
Classpath:.; % java_home % \ Lib \ DT. jar; % java_home % \ Lib \ tools. Jar

(2) tomcat6.0 environment variable Configuration:
Catalina_home: C: \ Program Files \ Apache Software Foundation \ Tomcat 6.0, where c: \ Program Files \ Apache Software Foundation is the installation directory of my tomcat 6.0.
In fact, you do not need to configure environment variables for tomcat6.0, because the latest version does not need to be configured.

(3) axis2 1.3 download: from [url] URLs. Decompress the file and copy the axis2.war file to the webapps directory of Tomcat to install axis2.
After the installation is complete, start tomcate and enter [url] http: // localhost: 8080/axis2/[/url] in the address bar. If the interface is displayed, the installation is correct:



(4) eclipse3.4 download: Go to the [url] www.eclipse.org [/url] official website to download the package. decompress the package and configure JDK in Windows/preference.

(5) download the eclipse axis2 plug-in: axis2-eclipse-codegen-wizard-1.4.zipand axis2-eclipse-codegen-wizard-1.4.zip. Yes: [url] http://apache.justdn.org/ws/axis2/tools/1_4_1/ [/url] These two plug-ins are extracted to the eclipse installation directory plugins. Open eclipse and select the file/New/other menu. The following interface indicates that the installation is successful:



The purpose of installing these two plug-ins is to easily generate the axis2 service and the axis2 client. Here I use a client without stub to call the service, so I will not demonstrate the function of generating the axis2 client.

2. Build services

After the development environment and the axis2 environment are set up, we can start the development of Web Services: 1. Create the Web service to be released


(1) Add a user library named axis2 in eclispse and add all the packages under axis2 \ Lib. The purpose of this step is to add the jar files required by axis2 to the project in the future.
(2) create a javaproject named WS and add the axis2 user library to build
Path.
(3) Now write the websevice to be released, build the briup package in SRC, and create the Hello class as follows:

package briup;

public class Hello {
  public String sayHello(String user) {
    return "Hello, " + user;
  }
}

2. Publish a Web Service

Package the service to be released, new in eclipse
--> File --> Other --> axis2 wizards --> axis2 services archiver, select the class file after the created class is compiled according to the wizard.
(1) Select the class file directory. Note that it is not a Java source file, but a classes directory. Note that because your class contains a briup package, do not select this directory.

(2) Press "next>" twice, select "generate the service XML automatically" (3), Press next, and enter the service name and class name, the service name I entered here is: Ws; the class name is the class name we just wrote: briup. hello, you must add the complete package name here. (4) Press next and enter the storage path and file name of the service file. Select the build Directory: C: \ Program Files \ Apache Software Foundation \ Tomcat 6.0 \ webapps \ axis2 \ WEB-INF \ Services, you can also select another directory, and then copy to this directory. 3. test Web Service open the [url] http: // localhost: 8080/axis2/services/listservices [/url] page, you can see the WS service, click to enter the ws wsdl page: [url] http: // localhost: 8080/axis2/services/WS? WSDL [/url] indicates that the service is correctly deployed. 3. Compile client code to call the serviceThis is the biggest difference between my example and other examples. For other examples, you need to generate the client stub according to the service WSDL and call the service through stub. This method is relatively simple, the client must use the stub to access the service. In this example, the client does not use the stub method, but is a universal call method that can access the service without any client stubs. You only need to specify the Web Servce address, operation name, parameter, and function return type. The Code is as follows:
package briup;

import javax.xml.namespace.QName;

import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

public class WsClient {

  private RPCServiceClient serviceClient;
  private Options options;
  private EndpointReference targetEPR;
  
  public WsClient (String endpoint) throws AxisFault {
    serviceClient = new RPCServiceClient ();
    options = serviceClient.getOptions ();
    targetEPR = new EndpointReference (endpoint);
    options.setTo (targetEPR);
  }
  public Object [] invokeOp (String targetNamespace, String opName,
      Object [] opArgs, Class <?> [] OpReturnType) throws AxisFault,
      ClassNotFoundException {
    // Set the name of the operation
    QName opQName = new QName (targetNamespace, opName);
    // Set the return value
    
    // Class <?> [] OpReturn = new Class [] {opReturnType};

    // The parameters that need to be passed in for the operation are given in the parameters. Here they are directly passed into the method to call
    return serviceClient.invokeBlocking (opQName, opArgs, opReturnType);
  }
  / **
    * @param args
    * @throws AxisFault
    * @throws ClassNotFoundException
    * /
  public static void main (String [] args) throws AxisFault, ClassNotFoundException {
    // TODO Auto-generated method stub
    final String endPointReference = "http: // localhost: 8080 / axis2 / services / ws";
    final String targetNamespace = "http: // briup";
    WsClient client = new WsClient (endPointReference);
    
    String opName = "sayHello";
    Object [] opArgs = new Object [] {"Repace Center"};
    Class <?> [] OpReturnType = new Class [] {String []. Class};
    
    Object [] response = client.invokeOp (targetNamespace, opName, opArgs, opReturnType);
    System.out.println (((String []) response [0]) [0]);
  }

} 

Run the program and click Run as-> JAVA application. The output of the Console port is hello. The repace Center indicates that the client has successfully called the program. The biggest difference and advantages of this example are the client call method, or the method for initiating a service call. Although the code is slightly more than the client's Stub stub method, this method is uniform, there is no need to produce the stub code, which solves many class problems on the client. If the reader further encapsulates the code, I think the call method is very simple, just passing the relevant parameters, which better demonstrates the advantages of service calling. In addition, this method is simpler and clearer. Instead of getting some mechanisms of stub classes.





Remarks: This article Reprinted from: http://panpan.blog.51cto.com/489034/119204/



Related Article

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.