Use axis2 to publish WebService and client calls

Source: Internet
Author: User






You have been successfully guaranteed !! Download instance: Click the open link

How to publish WebService with axis2

Axis2 + tomcat6.0 implement WebService Server Release and client call (-)

 

There are many methods for developing WebService in aixs2. Here we only introduce a simple implementation method.

Step 1: first download the jar package required for development

 

Download: axis2-1.6.1-war.zip

Http://www.apache.org/dist//axis/axis2/java/core/1.6.1/

Decompress the downloaded files to the webapps folder under the tomcat installation directory. After Tomcat is started, the axis2 folder is generated under the webapps directory.

Access http: // localhost: 8080/axis2/. The following page is displayed, indicating that axis2 runs successfully.


Part II

Create a web project under eclipse with the project name webserve. Create the com. cm. service package and create the class helloworld. The Code is as follows:

PackageCom. cm. Service;

 

Public classHelloworld {

 

PublicString sayhello (string name ){Return"Hello," + name + ".";}

PublicString saysorry (string name ){Return"Sorry," + name + ".";}

PublicString getworld (){Return"Hello, world ";}

}

 

Modify the Web. xml file in the WEB-INF directory with the following content:

<? Xmlversion ="1.0"Encoding =UTF-8"?>

<Web-appversion ="2.5">

<! -- Axis2ConfigStart -->

<Servlet>

<Servlet-Name> axisservlet </servlet-Name>

<Servlet-class> org. Apache. axis2.transport. http. axisservlet </servlet-class>

<Load-on-startup> 1 </load-on-startup>

</Servlet>

<Servlet-mapping>

<Servlet-Name> axisservlet </servlet-Name>

<URL-pattern>/services/* </url-pattern>

</Servlet-mapping>

 

<! -- Axis2 end -->

<Welcome-file-List>

<Welcome-File> index. jsp </welcome-File>

</Welcome-file-List>

</Web-app>

 

Copy the modules, service, and conf files under the webapps/axis2/WEB-INF under the tomcat installation directory to the WEB-INF directory under helloworld. Copy the following jar package in Lib.

 

Then create the helloworld/META-INF path under services, and create services. xml under the META-INF, the content is as follows:

<Servicename ="Helloworld">

<Description>

Helloworld service example

</Description>

<Parametername ="Serviceclass">

Com. cm. Service. helloworld

</Parameter>

<Operationname ="Sayhello">

<Messagereceiverclass ="Org. Apache. axis2.rpc. Receivers. rpcmessagereceiver"/>

</Operation>

<Operationname ="Saysorry">

<Messagereceiverclass ="Org. Apache. axis2.rpc. Receivers. rpcmessagereceiver"/>

</Operation>

<Operationname ="Getworld">

<Messagereceiverclass ="Org. Apache. axis2.rpc. Receivers. rpcinonlymessagereceiver"/>

</Operation>

</Service>

 

Start Tomcat and access http: // 127.0.0.1: 8888/webserve/services/helloworld? The service information is displayed in the WSDL.

The WebService of axis2 has been released successfully.

 


Axis2 client call instance


Axis2 + tomcat6.0 implement WebService Server Release and client call (2)

The following describes how to call an instance using the axis2 client.

Create a client call class axisutil

The Code is as follows:

PackageCom. cm. client;

ImportJavax. xml. namespace. QNAME;

ImportOrg. Apache. axis2.addressing. endpointreference;

ImportOrg. Apache. axis2.client. options;

ImportOrg. Apache. axis2.rpc. Client. rpcserviceclient;

 

Public classAxisutil {

Public static voidMain (string [] ARGs ){

String xmlstr = "Xiaoxu. Wang ";

String url = "http: // 127.0.0.1: 8888/webserve/services/helloworld ";

String method = "saysorry ";

Axisutil.Sendservice(Xmlstr, URL, method );

}

Public staticString sendservice (string xmlstr, string URL, string method ){

String xml =Null;

Rpcserviceclient serviceclient =NewRpcserviceclient ();

Options = serviceclient. getoptions ();

Endpointreference targetepr =NewEndpointreference (URL );

Options. setto (targetepr );

// When creating a QNAME object, the first parameter of the constructor of the QNAME class indicates the namespace name of the WSDL file, that is<WSDL: Definitions>Element targetnamespace Attribute Value

QNAME opaddentry =NewQNAME ("http://service.cm.com", method );

// Parameter. If there are multiple parameters, add them to the end without specifying the parameter name.

Object [] opaddentryargs =NewObject [] {xmlstr };

// Return parameter type, which is a little different from axis1

// The invokeblocking method has three parameters. The first parameter is a QNAME object, indicating the name of the method to be called;

// The second parameter indicates the parameter value of the WebService method to be called. The parameter type is object [].

// The third parameter indicates the Class Object of the WebService method's return value type. The parameter type is class [].

// When the method does not have a parameter, the second parameter value of the invokeblocking method cannot be null, but new object [] {} must be used.

// If the called WebService method does not return a value, use the invokerobust method of the rpcserviceclient class,

// This method has only two parameters. Their meanings are the same as those of the first two parameters of the invokeblocking method.

Class[] Classes =NewClass [] {string.Class};

Xml = (string) serviceclient. invokeblocking (opaddentry, opaddentryargs, classes) [0];

System.Out. Println (XML );

ReturnXML;

}

}

 

Running result:

Sorry, Xiaoxu. Wang.

 

Conclusion: The above is a simple case of axis2 Service Release and call. If you are interested in other implementation methods, you can continue to study.

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.