In today's Web applications, spring frameworks are often used to load JavaBean. If you want to publish some java beans assembled in spring to WebService, it is very easy to use the spring sensing function of axis2.
1. First, create a web project named WebService,
2. Test the JAR file of axis2 under the Lib of the WEB-INF.
3. The directory structure under the webroot of the project should be the same as the directory structure used in the war package (otherwise, an error may occur)
Directory structure:
4. Create package sample. Service under SRC
5. Create an interface to provide services
Package sample. Service;/*** define service interface * @ author 11111 **/public interface serviceserver {// define service method Public String sayhello (string name );}
Implementation class:
package sample.service;public class ServiceServerImpl implements ServiceServer {public String sayHello(String name) {return "hello"+name;}}
6. Create an applicationcontext. xml file under SRC.
The configuration is as follows:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"><bean id="SayHelloService" class="sample.service.ServiceServerImpl"></bean></beans>
7. Create the directory sampleservice under the webroor/WEB-INF/services/directory (this name can be obtained randomly)
Create the META-INF directory under it, and then create services. xml under its Directory
The directory structure is as follows:
The content of services. XML is as follows:
<? XML version = "1.0" encoding = "UTF-8"?> <Service name = "helloworld"> <description> Web Service </description> <parameter name = "serviceobjectsupplier"> Org. apache. axis2.extensions. spring. receivers. springservletcontextobjectsupplier </parameter> <parameter name = "springbeanname"> sayhelloservice </parameter> // The springbeanname name cannot be changed. // sayhelloservice is the implementation class ID registered in spring (you must be aware of this) <operation name = "sayhello"> <messagereceiverclass = "org. apache. axis2.rpc. receivers. rpcmessagereceiver "/> </Operation> </service>
8. Now you need to configure web. xml.
The content is as follows:
<? XML version = "1.0" encoding = "UTF-8"?> <Web-app version = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet-Name> axisservlet </servlet-Name> // register the servlet of axis2 <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> // load the spring configuration file <context-param> <param- name> contextconfiglocation </param-Name> <param-value> classpath *: applicationcontext. XML </param-value> </context-param> // Add a spring listener <listener> <listener-class> Org. springframework. web. context. contextloaderlistener </listener-class> </listener> </Web-app>
9. start Tomcat and enter http: // localhost: 8080/WebService/services/listservices in the browser.
We can see that our service has been released successfully.
Access
Http: // localhost: 8080/WebService/services/helloworld? WSDL
You can view the WSDL
Now we start to access our services. We use the axis2 Eclipse plug-in to automatically generate the client.
1. Automatic client Generation Based on WSDL
1. Create a Java project named serviceclient.
2. Create a user library named axis2 and add the JAR file required by axis2 to axis2.
Introduce this library in serviceclient
3. Install the axis2 plug-in eclipse
Service Archive wizard-Eclipse plug-in and code generator wizard-Eclipse plug-in
Note: The installation method is shown in the axis WebService notes in my blog. Install the eclipse axis2 plug-in (links mode) article.
4. (1) Choose new> Other> axis2 wizards> axis2 code generator.
(2) next step
(3) next step
Test the WSDL address in the browser.
(4) Click Next next.
(5) select to generate it to the src directory of the newly created serviceclient.
Then click Finish to generate the client and refresh the project.
5. generated directory structure:
2. Create serviceclient. Java as follows:
Package sample. service; public class serviceclient {/*** @ Param ARGs */public static void main (string [] ARGs) throws Java. lang. exception {// create a stub class helloworldstub stub = new helloworldstub (); // set the value of the corresponding method helloworldstub. sayhello = new helloworldstub. sayhello (); sayhello. setname ("Zhang San"); // call the corresponding method of the service and obtain the return value helloworldstub. sayhelloresponse response = stub. sayhello (sayhello); system. out. println (response. get_return ());}}
Run this Java class and you can see that
Print out: Hello Zhang San, which indicates that the call is successful.
Download the tutorial source code:
Axistest
Axisspring
Axisproject
Reprint please indicate the source http://blog.csdn.net/shimiso
Welcome to our technical exchange group: 173711587