第一步:
导入spring包,Axis2包及一些其他依赖包,我是把所有的包都导入了(此处省略一万个字.....)
第二步:
配置文件
1.web.xml
//spring的配置
<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>
//axis2的配置
<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>
2.applicationContext.xml的配置(只截取对webservice bean的配置)
<beans>
<bean id="weatherService"
class="sample.weatherservice.service.impl.WeatherService">
</bean>
</beans>
3.在WEB-INF\META-INF\里创建一个services.xml文件(注意必须在META-INF下)
内容如下:
<serviceGroup>//多个webservice时候用这个标签,这里带上也没事
<service name="WeatherService"> //webservice的代理类(业务实现类)继承IWeatherService
<description>WeatherService Spring POJO Axis2 deployment</description>
<parameter name="ServiceClass">
sample.weatherservice.service.IWeatherService //webservice接口
</parameter>
<parameter name="ServiceObjectSupplier">
org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier
</parameter>
//这个要与spring的配置文件里的bean一样(除非你的webservice的bean不是用spring管理的)
<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>
第三步:
1.webservice服务端代码(再简单不过的类/接口)
(1)webservice接口
package sample.weatherservice.service;
import sample.weatherservice.bean.Weather;
public interface IWeatherService {
String getWeather(boolean flag, String[] strArr) throws Exception;
}
(2) webservice代理类(业务实现类)
package sample.weatherservice.service.impl;
import sample.weatherservice.service.IWeatherService;
public class WeatherService implements IWeatherService {
public String getWeather(boolean flag, String[] strArr) throws Exception{
String returnStr="";
if(boolean) {
for(String s:strArr) {
returnStr+=s;
}
}
return returnStr;
}
}
<!--到此webservice的服务端就ok了-->
下面开启服务,在浏览器上输入Http://localhost:8080/services/WeatherService?wsdl 看会不会出现一个xml文件。若出现则表示成功了。
为了测试自己的webservice能不能用,可以写个java的客户端。如下(我这里是另起的一个java工程)
package com.sf.mem.stroe.api.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 org.apache.axis2.transport.http.HTTPConstants;
public class TestClent {
public static void main(String[] args1) throws AxisFault{
EndpointReference targetEPR = new EndpointReference(
"Http://localhost:8080/services/WeatherService");
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
options.setManageSession(true);
options.setProperty(HTTPConstants.REUSE_HTTP_CLIENT,true);
options.setTo(targetEPR);
QName opGetWeather = new QName("http://service.weatherservice.sample",
"getWeather");
String[] ss = {'aaA','bbB','ccC'};
Object[] opGetWeatherArgs = new Object[] {true,ss};//参数
Class[] returnTypes = new Class[] { String.class };//返回数据类型
Object[] response = serviceClient.invokeBlocking(opGetWeather,
opGetWeatherArgs, returnTypes);
serviceClient.cleanupTransport();
String result = (String) response[0];
if (result == null) {
System.out.println("Weather didn't initialize!");
} else {
System.out.println(result);
}
}
}
运行一下,测试ok
注意:这个客户端不是用wsdl文件生成的,所以是有问题的,为了能够让别人通过wsdl路径在eclipse里自动生成客户端,我在网上找了这么个帖子,简直是一条龙服务啊。
http://www.blogjava.net/tianchijiaozi/archive/2013/03/15/396452.html
本文出自 “重新来学JAVA” 博客,谢绝转载!