Implement two different client methods for axis2

Source: Internet
Author: User

1. A simple service class and publish it as Web
Service. Note: My project name is test.

Package test;


Public class testservice {

Public int add (int A, int B ){
Return A + B;
}

Public String each (string name ){
Return name + "hello ";
}

}


2. The application can call the client in two ways.

Import javax. xml. namespace. QNAME;


Import org. Apache. Axiom. om. omw.actfactory;
Import org. Apache. Axiom. om. omelement;
Import org. Apache. Axiom. om. omfactory;
Import org. Apache. Axiom. om. omnamespace;
Import org. Apache. axis2.axisfault;
Import org. Apache. axis2.addressing. endpointreference;
Import org. Apache. axis2.client. options;
Import org. Apache. axis2.client. serviceclient;
Import org. Apache. axis2.rpc. Client. rpcserviceclient;

/**
* Axis2 provides the implementation of RPC and document styles.
* Here we do not verify it. For more information, see comments in the code.
* @ Author Administrator
*
*/
Public class client {

Public static void main (string [] ARGs ){

Client client = new client ();
// Test the RPC Method
Client. testrpc ();

// Test the document Method
Client. testdocument ();
}

/**
* Call by using RPC
* This method is equivalent to remote call, that is, to tell the remote server through URL location, to inform the method name, parameters, etc,
* Call the remote service to obtain the result.
*/
// The annotaion below is used to prevent the code from warning that no generic application is available.
// Elipse is easy to understand.
@ Suppresswarnings ("unchecked ")
Public void testrpc (){
Try {
Rpcserviceclient serviceclient = new rpcserviceclient ();
Options = serviceclient. getoptions ();
// Specify the accessed Web service address
Endpointreference targetepr = new endpointreference (
"Http: // localhost: 8080/test/services/testservice ");
Options. setto (targetepr );

// Specify the method. Specify the namespace.
QNAME opprint = new QNAME ("http: // test ",
"Add ");
// Determine the parameter type and value
Class [] returntypes = new class [] {Int. Class };
Object OBJ [] = new object [] {1, 2 };
// Get the returned result, which is an array
Object [] Order = serviceclient. invokeblocking (opprint, OBJ,
Returntypes );
System. Out. println (Order [0]);


// The following describes how to test the each method. Similar to the above
Opprint = new QNAME ("http: // test", "each ");
Returntypes = new class [] {string. Class };
OBJ = new object [] {"zhangyt "};
Order = serviceclient. invokeblocking (opprint, OBJ,
Returntypes );
System. Out. println (Order [0]);

} Catch (axisfault e ){
E. printstacktrace ();
}
}

/**
* Application document method call
* Ducument applications are cumbersome and flexible. Currently, many instances are used. This is because it truly gets rid of the coupling we don't want.
*/
Public void testdocument (){
Try {
Serviceclient SC = new serviceclient ();
Options opts = new options ();
// Determine the target service address
Opts. setto (New endpointreference (
"Http: // localhost: 8080/test/services/testservice "));
// Determine the call Method
Opts. setaction ("urn: Add ");
SC. setoptions (OPTs );
// Send the request and obtain the returned result. Pay attention to the analysis of the parameter generation method.
Omelement res = SC. sendreceive (createpayload ());
// It is worth noting that the returned result is an XML string encapsulated by the omelement object.
// We can apply it flexibly. Next I will take the first element value and print it. Because the called method returns a result
Res. getfirstelement (). gettext ();
System. Out. println (res. getfirstelement (). gettext ());
} Catch (axisfault e ){
E. printstacktrace ();
}
}
/**
* Generate parameters of the sendreceive (omelement ARGs) method in serviceclient
* @ Return
*/
Public static omelement createpayload (){
Omfactory FAC = omabstractfactory. getomfactory ();
// Specify the namespace
Omnamespace omns = FAC. createomnamespace ("http: // test", "NSL ");
// Specify the method
Omelement method = FAC. createomelement ("add", omns );
// Specify method parameters
Omelement value = FAC. createomelement ("value", omns );
Value. settext ("1 ");
Method. addchild (value );
Omelement value1 = FAC. createomelement ("value", omns );
Value1.settext ("2 ");
Method. addchild (value1 );
// Return method (in fact, it is the XML string encapsulated by omelement)
Return method;
}

}



Note that only the add method in the service class is tested when the document method is called. This method is used, and the each method is easier to call than this method.

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.