spring下的webservice之xfire
http://xfire.codehaus.org/
xfire的下载站点。下载了xfire,结合本机上的spring配置文件,做如下配置:
在spring的配置文件applicationContext-webservice.xml中增加如下配置:
<!--xfirewebservice-->
<!--xfire默认配置文件,存在jar包中-->
<importresource="classpath:org/codehaus/xfire/spring/xfire.xml"/>
<beanid="baseWebService"class="org.codehaus.xfire.spring.remoting.XFireExporter"lazy-init="false"
abstract="true">
<propertyname="serviceFactory"ref="xfire.serviceFactory"/>
<propertyname="xfire"ref="xfire"/>
</bean>
<!--webService的接口类-->
<beanname="IUserManager"parent="baseWebService">
<propertyname="serviceBean"ref="xfireUserService"/>
<propertyname="serviceClass"value="com.sillycat.core.webservice.IUserManager"/>
</bean>
<!--webService的包装实现类-->
<beanname="xfireUserService"class="com.sillycat.core.webservice.impl.XfireUserService"/>
在WebRoot/WEB-INF/web.xml中增加如下配置:
<!--xfirewebservice-->
<servlet>
<servlet-name>XFireServlet</servlet-name>
<servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>XFireServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
接口和以前的axis的接口一致,嘿嘿。我这次配置的还是axis和xfire可以混用的,不过一般项目中怎么可能两个混用呢。只是学习的时候用用吧。
IUserManager.java:
packagecom.sillycat.core.webservice;
importjava.util.List;
importcom.sillycat.core.webservice.model.IUser;
publicinterfaceIUserManager{
publicList<IUser>getAllUser();
publicIUsergetUser(Integerid);
publicvoidsaveUser(IUseruser);
publicvoidremoveUser(Integerid);
}
XfireUserService实现类,XfireUserService.java:
packagecom.sillycat.core.webservice.impl;
importjava.util.List;
importcom.sillycat.core.model.User;
importcom.sillycat.core.service.UserManager;
importcom.sillycat.core.webservice.IUserManager;
importcom.sillycat.core.webservice.model.IUser;
importcom.sillycat.core.webservice.transformer.UserTransformer;
publicclassXfireUserServiceimplementsIUserManager{
privateUserManageruserManager;
publicList<IUser>getAllUser(){
returnuserManager.getAllUser();
}
publicIUsergetUser(Integerid){
Useru=userManager.getUser(id);
IUseri=UserTransformer.transformUser2IUser(u);
returni;
}
publicvoidremoveUser(Integerid){
userManager.removeUser(id);
}
publicvoidsaveUser(IUseruser){
//TODO
}
publicvoidsetUserManager(UserManageruserManager){
this.userManager=userManager;
}
}
这样server端就配置好了。如果访问这个地址能访问到WSDL文件,就成功了:
http://www.sillycat.com/easyLife/services/IUserManager?wsdl
http://www.sillycat.com/easyLife是地址
services/IUserManager?wsdl是提供出来的接口名字
客户端调用用myeclipse可以生成,用springside上提供的client的方式用也可以,不过都觉得不爽。稍微修改了一下,参考axis的客户端调用
首先写了一个调用的工具类XFireClientFactory.java,把calvin的类改了改,继承自FactoryBean接口:
packagecom.sillycat.plugin.webservice.xfire;
importjava.net.MalformedURLException;
importorg.apache.commons.logging.Log;
importorg.apache.commons.logging.LogFactory;
importorg.codehaus.xfire.annotations.AnnotationServiceFactory;
importorg.codehaus.xfire.client.XFireProxyFactory;
importorg.codehaus.xfire.service.Service;
importorg.codehaus.xfire.service.binding.ObjectServiceFactory;
importorg.springframework.beans.factory.FactoryBean;
importorg.springframework.util.Assert;
/**
*XFireClient工厂�?.<p/>
*<p>
*封装了XFire有点奇�?�的根据Web服务接口类和ServiceURL生成客户端Proxy类的代码.同时使用泛型,减少了返回时的强制类型转�?.
*</p>
*
*@authorcalvin
*/
@SuppressWarnings("unchecked")
publicclassXFireClientFactoryimplementsFactoryBean{
privatestaticXFireProxyFactoryserviceFactory=newXFireProxyFactory();
privatestaticfinalLoglog=LogFactory.getLog(XFireClientFactory.class);
privateStringserviceURL;
privateStringserviceClassName;
privateXFireClientFactory(){
}
publicObjectgetObject()throwsException{
Stringurl=this.getServiceURL();
ClasssClass=null;
try{
sClass=Class.forName(this.getServiceClassName());
}catch(ClassNotFoundExceptione){
log.error(e.getMessage(),e);
returnnull;
}
Assert.notNull(url);
Assert.notNull(sClass);
ServiceserviceModel=newObjectServiceFactory().create(sClass);
try{
returnserviceFactory.create(serviceModel,url);
}catch(MalformedURLExceptione){
log.error(e.getMessage(),e);
returnnull;
}
}
publicClassgetObjectType(){
ClasssClass=null;
try{
sClass=Class.forName(this.getServiceClassName());
}catch(ClassNotFoundExceptione){
log.error(e.getMessage(),e);
returnnull;
}
returnsClass;
}
publicbooleanisSingleton(){
returntrue;
}
/**
*获得POJO形式的Web服务的客户端Proxy�?.
*
*@paramserviceURL
*WebServiceURL
*@paramserviceClass
*WebService接口�?
*@return类型为WebService接口的客户端Proxy�?
*/
publicstatic<T>TgetClient(StringserviceURL,Class<T>serviceClass){
Assert.notNull(serviceURL);
Assert.notNull(serviceClass);
ServiceserviceModel=newObjectServiceFactory().create(serviceClass);
try{
return(T)serviceFactory.create(serviceModel,serviceURL);
}catch(MalformedURLExceptione){
log.error(e.getMessage(),e);
returnnull;
}
}
/**
*获得JSR181形式定义的Web服务的客户端Proxy�?.注意和普通的生成客户端方法不�?,�?要多�?个implClass参数.
*
*@paramserviceURL
*WebServiceURL.
*@paramserviceClass
*WebService接口�?.
*@paramimplClass
*WebService街口的服务端实现�?.
*@return类型为WebService接口的客户端Proxy�?.
*/
publicstatic<T>TgetJSR181Client(StringserviceURL,
Class<T>serviceClass,ClassimplClass){
Assert.notNull(serviceURL);
Assert.notNull(serviceClass);
ServiceserviceModel=newAnnotationServiceFactory().create(implClass);
try{
return(T)serviceFactory.create(serviceModel,serviceURL);
}catch(MalformedURLExceptione){
log.error(e.getMessage(),e);
returnnull;
}
}
publicStringgetServiceURL(){
returnserviceURL;
}
publicvoidsetServiceURL(StringserviceURL){
this.serviceURL=serviceURL;
}
publicStringgetServiceClassName(){
returnserviceClassName;
}
publicvoidsetServiceClassName(StringserviceClassName){
this.serviceClassName=serviceClassName;
}
}
上面这个客户端调用接口类,其实一般都是不变了。每次新增一个调用就新增一个spring的配置就可以了,还是在
applicationContext-webservice.xml这个配置里面增加如下几行:
<beanname="userManagerXfire"
class="com.sillycat.plugin.webservice.xfire.XFireClientFactory">
<propertyname="serviceURL"value="http://www.sillycat.com/easyLife/services/IUserManager"/>
<propertyname="serviceClassName"value="com.sillycat.core.webservice.IUserManager"/>
</bean>
serviceURL是提供webservice的地址
serviceClassName是webservice这个类的接口。呵呵。这种方法调用默认要把接口类打包到调用webservice那边去哈。
接口发布给别人不算泄密吧。