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,增加配置:
- <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>
新建一个JSP:/ws-sample/WebRoot/axis2-web/listServices.jsp
- <%@
- page contentType="text/html;charset=UTF-8" language="java"
- %><%@
- page import="org.apache.axis2.Constants,
- org.apache.axis2.description.AxisOperation,
- org.apache.axis2.description.AxisService,
- java.util.Collection,
- java.util.HashMap,
- java.util.Iterator"
- %><html>
- <head><title>List Services</title>
- <style>
- h2{margin:20 0 5 0;}
- ul{margin-top:5;}
- </style>
- </head>
- <body>
- <h1>Available services</h1>
- <%
- HashMap serviceMap = (HashMap) request.getSession().getAttribute(Constants.SERVICE_MAP);
- Collection servicecol = serviceMap.values();
- if(servicecol.size()==0){%>Available services is Empty.<%}
- for (Iterator iterator = servicecol.iterator(); iterator.hasNext();) {
- AxisService axisService = (AxisService) iterator.next();
- Iterator opItr = axisService.getOperations();
- String serviceName = axisService.getName();
- %>
- <h2><font color="blue"><a href="<%=serviceName %>?wsdl" target="_blank"><%=serviceName%></a></font></h2>
- <i>Available Operations</i>
- <ul>
- <%
- while (opItr.hasNext()) {
- AxisOperation axisOperation = (AxisOperation) opItr.next();
- %><li><%=axisOperation.getName().getLocalPart()%></li><%
- }
- %>
- </ul>
- <%
- }
- %>
- </body>
- </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
代码如下:
- //Weather.java
- package sample.weatherservice.bean;
- public class Weather {
- float temperature;
- String forecast;
- boolean rain;
- float howMuchRain;
- public void setTemperature(float temp) {
- temperature = temp;
- }
- public float getTemperature() {
- return temperature;
- }
- public void setForecast(String fore) {
- forecast = fore;
- }
- public String getForecast() {
- return forecast;
- }
- public void setRain(boolean r) {
- rain = r;
- }
- public boolean getRain() {
- return rain;
- }
- public void setHowMuchRain(float howMuch) {
- howMuchRain = howMuch;
- }
- public float getHowMuchRain() {
- return howMuchRain;
- }
- }
- //IWeatherService.java
- package sample.weatherservice.service;
- import sample.weatherservice.bean.Weather;
- public interface IWeatherService {
- void setWeather(Weather w);
- Weather getWeather();
- }
- //WeatherService.java
- package sample.weatherservice.service.impl;
- import sample.weatherservice.bean.Weather;
- import sample.weatherservice.service.IWeatherService;
- public class WeatherService implements IWeatherService {
- Weather weather;
- public void setWeather(Weather w) {
- weather = w;
- }
- public Weather getWeather() {
- return weather;
- }
- }
新建spring配置文件:
ws-sample/WebRoot/WEB-INF/applicationContext.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
- <beans>
- <bean id="weatherService" class="sample.weatherservice.service.impl.WeatherService">
- <property name="weather">
- <bean class="sample.weatherservice.bean.Weather">
- <property name="temperature" value="89.9" />
- <property name="forecast" value="Sunny" />
- <property name="rain" value="false" />
- <property name="howMuchRain" value="0.2" />
- </bean>
- </property>
- </bean>
- </beans>
修改 ws-sample/WebRoot/WEB-INF/web.xml 增加:
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/applicationContext.xml</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
在 ws-sample/WebRoot/WEB-INF/services/ 目录下,新建文件夹和文件 weatherservice/META-INF/services.xml
services.xml的内容如下:
- <serviceGroup>
- <service name="WeatherService">
- <description>WeatherService:Spring POJO Axis2 Service Sample</description>
- <parameter name="ServiceClass">sample.weatherservice.service.IWeatherService</parameter>
- <parameter name="ServiceObjectSupplier">
- org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier
- </parameter>
- <parameter name="SpringBeanName">weatherService</parameter>
- <messageReceivers>
- <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
- class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
- </messageReceivers>
- </service>
- </serviceGroup>
刷新 http://localhost:8080/ws-sample/services/listServices
见到新增了一个叫WeatherService的服务,说明 WeatherService 已成功部署
5.开发客户端调用
创建类:client.WeatherRPCClient
- package client;
- import javax.xml.namespace.QName;
- import org.apache.axis2.AxisFault;
- import org.apache.axis2.addressing.EndpointReference;
- import org.apache.axis2.client.Options;
- import org.apache.axis2.rpc.client.RPCServiceClient;
- import sample.weatherservice.bean.Weather;
- public class WeatherRPCClient {
- public static void main(String[] args1) throws AxisFault {
- EndpointReference targetEPR = new EndpointReference("http://localhost:8080/ws-sample/services/WeatherService");
- RPCServiceClient serviceClient = new RPCServiceClient();
- Options options = serviceClient.getOptions();
- options.setTo(targetEPR);
- QName opGetWeather = new QName("http://service.weatherservice.sample", "getWeather");
- Object[] opGetWeatherArgs = new Object[] { };
- Class[] returnTypes = new Class[] { Weather.class };
- Object[] response = serviceClient.invokeBlocking(opGetWeather,opGetWeatherArgs, returnTypes);
- Weather result = (Weather) response[0];
- if (result == null) {
- System.out.println("Weather didn't initialize!");
- }else{
- System.out.println();
- System.out.println("Temperature : " + result.getTemperature());
- System.out.println("Forecast : " + result.getForecast());
- System.out.println("Rain : " + result.getRain());
- System.out.println("How much rain (in inches) : " + result.getHowMuchRain());
- }
- }
- }
运行 WeatherRPCClient,输出如下,说明调用成功:
Temperature : 89.9
Forecast : Sunny
Rain : false
How much rain (in inches) : 0.2