在現今的Web應用中經常使用Spring架構來裝載JavaBean。如果要想將某些在Spring中裝配的JavaBean發布成WebService,使用Axis2的Spring感知功能是非常容易做到的。
1、首先建立一個web工程,名字叫WebService,
2、把相應的axis2的jar檔案考到WEB-INF的lib下
3、 在項目的WebRoot下的目錄結構要和以前用war包是的目錄結構一樣(否則可能就要報錯了)
目錄結構:
4、在src下建立package sample.service
5、建立提供服務的介面
package sample.service;/** * 定義服務介面 * @author 11111 * */public interface ServiceServer {//定義服務方法public String sayHello(String name);}
實作類別:
package sample.service;public class ServiceServerImpl implements ServiceServer {public String sayHello(String name) {return "hello"+name;}}
6、在src下建立applicationContext.xml檔案
配置如下
<?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、在WebRoor/WEB-INF/services/目錄下建立目錄sampleService(這個名字可以隨便取)
然後建立在其下META-INF目錄,然後再在其目錄下建立services.xml
目錄結構如下
services.xml的內容如下:
<?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>//SpringBeanName名字是固定的不能改//SayHelloService是spring中註冊的實作類別的id(這個大家肯定很清楚了) <operation name="sayHello"><messageReceiverclass="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /></operation></service>
8、現在要配置一下web.xml了
內容如下:
<?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> <servlet-name>AxisServlet</servlet-name>//註冊axis2的servlet <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>//載入spring的設定檔<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext.xml</param-value></context-param>//增加spring監聽器<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener></web-app>
9、啟動tomcat 在瀏覽器中輸入http://localhost:8080/WebService/services/listServices
可以看到一下內容說明我們的服務已經發布成功了
訪問
http://localhost:8080/WebService/services/HelloWorld?wsdl
可以查看wsdl
現在我們開始訪問我們的服務,我們採用用axis2 的eclipse 外掛程式自動產生用戶端
一、根據wsdl自動產生用戶端的方式
1、建立一個java project ,取名為ServiceClient
2、建立一個User Library 取名AXIS2 將axis2 所需要的jar檔案加到AXIS2中,然後在
ServiceClient 中引入這個library
3、在eclipse中安裝axis2外掛程式
Service Archive Wizard - Eclipse Plug-in 和 Code Generator Wizard - Eclipse Plug-in
註:安裝方式可見我的部落格中的axis webservice 筆記 安裝eclipse axis2 外掛程式 (links 方式)文章
4、(1)選擇new->other->Axis2 Wizards-> Axis2 code generator
(2)下一步
(3)下一步
把瀏覽其中的wsdl地址考到裡面
(4) 下一步 直接點next
(5)選擇產生到我們剛建好的ServiceClient的src 目錄中
然後點擊finish,這樣就可一產生用戶端了,重新整理項目就可以看到了
5、產生的目錄結構:
二、建立ServiceClient.java 內容如下
package sample.service;public class ServiceClient {/** * @param args */public static void main(String[] args) throws java.lang.Exception{//建立存根類HelloWorldStub stub = new HelloWorldStub();//設定相應的方法的值 HelloWorldStub.SayHello sayHello = new HelloWorldStub.SayHello();sayHello.setName("張三");//調用服務的相應方法並獲得傳回值HelloWorldStub.SayHelloResponse response = stub.sayHello(sayHello);System.out.println(response.get_return());}}
運行這個java類,可以看到console中
列印出:hello張三 ,說明我們已經調用成功了。
教程源碼下載:
AxisTest
AxisSpring
AxisProject
轉載請標明出處http://blog.csdn.net/shimiso
歡迎有識之士加入我們的技術交流群:173711587