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

Source: Internet
Author: User

Transferred fromHttp://hi.baidu.com/getpost/blog/item/eb9187817b4cb1d2bc3e1ee4.html


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. In eclipse, choose new --> 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.

(Class file location: \ working directory \ Project name \ ws \ bin)
(2) Press "next>" twice and select "generate the service XML automatically"
(3) Press next and enter the service name and class name. Here, the service name is WS, and 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: Tomcat 6.0 directory \ webapps \ axis2 \ WEB-INF \ Services, you can also select another directory, and then copy to this directory.


 3. Test Web Service

Open the http: // localhost: 8080/axis2/services/listservices page, you can see the WS service, click to enter the ws wsdl page: http: // localhost: 8080/axis2/services/WS? WSDL, indicating that the service is correctly deployed.

(Premise: tomcat has been started)


3. Compile client code to call the service

This 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;
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 operation name
QNAME opqname = new QNAME (targetnamespace, opname );
// Set the return value

// Class <?> [] Opreturn = new class [] {opreturntype };

// The parameter to be passed in for the Operation has been specified in the parameter. Here, the parameter is directly called in the method.
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.


4. Test: callbackhandler. Java and accountservicestub. Java are generated through the WSDL file.

1) New --> file --> Other --> axis2 wizards --> axis2 Code Generator

2) Select generate Java source code from a WSDL file and click Next

3) Press browse to select or enter the location of the WSDL file, and then click Next.
So far, we have not compiled and generated the WSDL file on our own. After the Web Service is published, you can access http: // localhost: 8080/axis2/services/WS? The WSDL file can be obtained. In this case, you can directly enter the URL in the address bar:

4) use the default configuration and click Next. In the new window that appears, select the storage path of the generated file to complete the generation of two java files.

5) in the project directory, press F5 to refresh and you will see the generated file.

6) write the service test program wstest. Java as follows:



 package briup;

public class WsTest {

public static void main(String[] args) throws Exception {

WsStub stub = new WsStub("http://localhost:8080/axis2/services/ws");

WsStub.SayHello sayHello = new WsStub.SayHello();


WsStub.SayHelloResponse res = stub.sayHello(sayHello);






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.