1.下载 spring-framework-2.0.8.zip 和 axis2-1.5-war.zip 备用:
http://nchc.dl.sourceforge.net/project/springframework/springframework-2/2.0.8/spring-framework-2.0.8.zip
http://apache.etoak.com/ws/axis2/1_5/axis2-1.5-war.zip


2.新建一个web工程:ws-sample

解压pring-framework-2.0.8.zip 和 axis2-1.5-war.zip
将 spring.jar 和 axis2/WEB-INF/lib 里的jar包拷贝到 ws-sample/WebRoot/WEB-INF/lib/

打开ws-sample/WebRoot/WEB-INF/web.xml,增加配置:

Xml代码   转载:Spring + axis2 开发 webservice_spring
  1. <servlet>  
  2.     <servlet-name>AxisServlet</servlet-name>  
  3.     <servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>  
  4.     <load-on-startup>1</load-on-startup>  
  5. </servlet>  
  6.   
  7. <servlet-mapping>  
  8.     <servlet-name>AxisServlet</servlet-name>  
  9.         <url-pattern>/services/*</url-pattern>  
  10. </servlet-mapping>  

新建一个JSP:/ws-sample/WebRoot/axis2-web/listServices.jsp

Jsp代码   转载:Spring + axis2 开发 webservice_spring
  1. <%@   
  2. page contentType="text/html;charset=UTF-8" language="java"   
  3. %><%@  
  4. page import="org.apache.axis2.Constants,  
  5.             org.apache.axis2.description.AxisOperation,  
  6.             org.apache.axis2.description.AxisService,  
  7.             java.util.Collection,  
  8.             java.util.HashMap,  
  9.             java.util.Iterator"   
  10. %><html>  
  11. <head><title>List Services</title>  
  12. <style>  
  13. h2{margin:20 0 5 0;}  
  14. ul{margin-top:5;}  
  15. </style>  
  16. </head>  
  17. <body>  
  18. <h1>Available services</h1>  
  19. <%  
  20.     HashMap serviceMap = (HashMap) request.getSession().getAttribute(Constants.SERVICE_MAP);          
  21.     Collection servicecol = serviceMap.values();  
  22.     if(servicecol.size()==0){%>Available services is Empty.<%}  
  23.     for (Iterator iterator = servicecol.iterator(); iterator.hasNext();) {  
  24.         AxisService axisService = (AxisService) iterator.next();  
  25.         Iterator opItr = axisService.getOperations();  
  26.         String serviceName = axisService.getName();  
  27.   
  28. %>  
  29.   
  30. <h2><font color="blue"><a href="<%=serviceName %>?wsdl" target="_blank"><%=serviceName%></a></font></h2>  
  31. <i>Available Operations</i>  
  32. <ul>  
  33. <%  
  34. while (opItr.hasNext()) {  
  35.     AxisOperation axisOperation = (AxisOperation) opItr.next();  
  36.     %><li><%=axisOperation.getName().getLocalPart()%></li><%  
  37. }  
  38. %>  
  39. </ul>  
  40.   
  41. <%  
  42.     }  
  43. %>  
  44. </body>  
  45. </html>  

 

部署至tomcat,然后访问:
http://localhost:8080/ws-sample/services/listServices
如果不出差错的话,可以看到 Available services is Empty

 

3.部署pojo服务
新建目录:ws-sample/WebRoot/WEB-INF/services/

将 axis2/WEB-INF/services/version.aar 拷贝至 ws-sample/WebRoot/WEB-INF/services/

刷新 http://localhost:8080/ws-sample/services/listServices
见到一个叫Version的服务,说明 version.aar 已成功部署

 

4.开发并部署基于 Spring ApplicationContex 的服务
创建接口:sample.weatherservice.service.IWeatherService
和类:
sample.weatherservice.bean.Weather
sample.weatherservice.service.impl.WeatherService
代码如下:

Java代码   转载:Spring + axis2 开发 webservice_spring
  1. //Weather.java  
  2. package sample.weatherservice.bean;  
  3.   
  4. public class Weather {  
  5.     float temperature;  
  6.     String forecast;  
  7.     boolean rain;  
  8.     float howMuchRain;  
  9.   
  10.     public void setTemperature(float temp) {  
  11.         temperature = temp;  
  12.     }  
  13.   
  14.     public float getTemperature() {  
  15.         return temperature;  
  16.     }  
  17.   
  18.     public void setForecast(String fore) {  
  19.         forecast = fore;  
  20.     }  
  21.   
  22.     public String getForecast() {  
  23.         return forecast;  
  24.     }  
  25.   
  26.     public void setRain(boolean r) {  
  27.         rain = r;  
  28.     }  
  29.   
  30.     public boolean getRain() {  
  31.         return rain;  
  32.     }  
  33.   
  34.     public void setHowMuchRain(float howMuch) {  
  35.         howMuchRain = howMuch;  
  36.     }  
  37.   
  38.     public float getHowMuchRain() {  
  39.         return howMuchRain;  
  40.     }  
  41. }  

 

Java代码   转载:Spring + axis2 开发 webservice_spring
  1. //IWeatherService.java  
  2. package sample.weatherservice.service;  
  3.   
  4. import sample.weatherservice.bean.Weather;  
  5.   
  6. public interface IWeatherService {  
  7.     void setWeather(Weather w);  
  8.   
  9.     Weather getWeather();  
  10. }  

 

Java代码   转载:Spring + axis2 开发 webservice_spring
  1. //WeatherService.java  
  2. package sample.weatherservice.service.impl;  
  3.   
  4. import sample.weatherservice.bean.Weather;  
  5. import sample.weatherservice.service.IWeatherService;  
  6.   
  7. public class WeatherService implements IWeatherService {  
  8.     Weather weather;  
  9.   
  10.     public void setWeather(Weather w) {  
  11.         weather = w;  
  12.     }  
  13.   
  14.     public Weather getWeather() {  
  15.         return weather;  
  16.     }  
  17. }  

 
新建spring配置文件:
ws-sample/WebRoot/WEB-INF/applicationContext.xml

Xml代码   转载:Spring + axis2 开发 webservice_spring
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  
  3. <beans>  
  4.     <bean id="weatherService" class="sample.weatherservice.service.impl.WeatherService">  
  5.         <property name="weather">  
  6.             <bean class="sample.weatherservice.bean.Weather">  
  7.                 <property name="temperature" value="89.9" />  
  8.                 <property name="forecast" value="Sunny" />  
  9.                 <property name="rain" value="false" />  
  10.                 <property name="howMuchRain" value="0.2" />  
  11.             </bean>  
  12.         </property>  
  13.     </bean>  
  14. </beans>  

 

修改 ws-sample/WebRoot/WEB-INF/web.xml 增加:

Xml代码   转载:Spring + axis2 开发 webservice_spring
  1. <context-param>  
  2.     <param-name>contextConfigLocation</param-name>  
  3.     <param-value>/WEB-INF/applicationContext.xml</param-value>  
  4. </context-param>  
  5.   
  6. <listener>  
  7.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  8. </listener>  

 

在 ws-sample/WebRoot/WEB-INF/services/ 目录下,新建文件夹和文件 weatherservice/META-INF/services.xml
services.xml的内容如下:

Xml代码   转载:Spring + axis2 开发 webservice_spring
  1. <serviceGroup>  
  2.     <service name="WeatherService">  
  3.         <description>WeatherService:Spring POJO Axis2 Service Sample</description>  
  4.         <parameter name="ServiceClass">sample.weatherservice.service.IWeatherService</parameter>  
  5.         <parameter name="ServiceObjectSupplier">  
  6.             org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier  
  7.         </parameter>  
  8.         <parameter name="SpringBeanName">weatherService</parameter>  
  9.         <messageReceivers>  
  10.             <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"  
  11.                 class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />  
  12.         </messageReceivers>  
  13.     </service>  
  14. </serviceGroup>  

 刷新 http://localhost:8080/ws-sample/services/listServices
见到新增了一个叫WeatherService的服务,说明 WeatherService 已成功部署

 

5.开发客户端调用

创建类:client.WeatherRPCClient

Java代码   转载:Spring + axis2 开发 webservice_spring
  1. package client;  
  2.   
  3. import javax.xml.namespace.QName;  
  4. import org.apache.axis2.AxisFault;  
  5. import org.apache.axis2.addressing.EndpointReference;  
  6. import org.apache.axis2.client.Options;  
  7. import org.apache.axis2.rpc.client.RPCServiceClient;  
  8. import sample.weatherservice.bean.Weather;  
  9.   
  10. public class WeatherRPCClient {  
  11.   
  12.     public static void main(String[] args1) throws AxisFault {  
  13.           
  14.         EndpointReference targetEPR = new EndpointReference("http://localhost:8080/ws-sample/services/WeatherService");  
  15.         RPCServiceClient serviceClient = new RPCServiceClient();  
  16.         Options options = serviceClient.getOptions();  
  17.         options.setTo(targetEPR);  
  18.   
  19.         QName opGetWeather = new QName("http://service.weatherservice.sample""getWeather");  
  20.         Object[] opGetWeatherArgs = new Object[] { };  
  21.         Class[] returnTypes = new Class[] { Weather.class };  
  22.         Object[] response = serviceClient.invokeBlocking(opGetWeather,opGetWeatherArgs, returnTypes);  
  23.   
  24.         Weather result = (Weather) response[0];  
  25.         if (result == null) {  
  26.             System.out.println("Weather didn't initialize!");  
  27.         }else{  
  28.             System.out.println();  
  29.             System.out.println("Temperature               : " + result.getTemperature());  
  30.             System.out.println("Forecast                  : " + result.getForecast());  
  31.             System.out.println("Rain                      : " + result.getRain());  
  32.             System.out.println("How much rain (in inches) : " + result.getHowMuchRain());  
  33.         }  
  34.     }  
  35. }  

 

运行 WeatherRPCClient,输出如下,说明调用成功:
Temperature               : 89.9
Forecast                  : Sunny
Rain                      : false
How much rain (in inches) : 0.2