当前位置: 代码迷 >> Web前端 >> spring上的webservice之xfire
  详细解决方案

spring上的webservice之xfire

热度:100   发布时间:2012-11-14 10:12:18.0
spring下的webservice之xfire
http://xfire.codehaus.org/

xfire的下载站点。下载了xfire,结合本机上的spring配置文件,做如下配置:

在spring的配置文件applicationContext-webservice.xml中增加如下配置:
<!-- xfire webservice -->
<!--   xfire默认配置文件,存在jar包中 -->
<import resource="classpath:org/codehaus/xfire/spring/xfire.xml"/>

<bean id="baseWebService" class="org.codehaus.xfire.spring.remoting.XFireExporter" lazy-init="false"
    abstract="true">
   <property name="serviceFactory" ref="xfire.serviceFactory"/>
   <property name="xfire" ref="xfire"/>
</bean>

<!-- webService的接口类 -->
<bean name="IUserManager" parent="baseWebService">
   <property name="serviceBean" ref="xfireUserService"/>
   <property name="serviceClass" value="com.sillycat.core.webservice.IUserManager"/>
</bean>

<!-- webService的包装实现类 -->
<bean name="xfireUserService" class="com.sillycat.core.webservice.impl.XfireUserService" />

在WebRoot/WEB-INF/web.xml中增加如下配置:
<!-- xfire webservice -->
<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 :

package com.sillycat.core.webservice;

import java.util.List;

import com.sillycat.core.webservice.model.IUser;

public interface IUserManager {

public List<IUser> getAllUser();

public IUser getUser(Integer id);

public void saveUser(IUser user);

public void removeUser(Integer id);
}

XfireUserService实现类,XfireUserService.java:

package com.sillycat.core.webservice.impl;

import java.util.List;

import com.sillycat.core.model.User;
import com.sillycat.core.service.UserManager;
import com.sillycat.core.webservice.IUserManager;
import com.sillycat.core.webservice.model.IUser;
import com.sillycat.core.webservice.transformer.UserTransformer;

public class XfireUserService implements IUserManager {

private UserManager userManager;

public List<IUser> getAllUser() {
   return userManager.getAllUser();
}

public IUser getUser(Integer id) {
   User u = userManager.getUser(id);
   IUser i = UserTransformer.transformUser2IUser(u);
   return i;
}

public void removeUser(Integer id) {
   userManager.removeUser(id);
}

public void saveUser(IUser user) {
   //TODO
}

public void setUserManager(UserManager userManager) {
   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接口:

package com.sillycat.plugin.webservice.xfire;

import java.net.MalformedURLException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.xfire.annotations.AnnotationServiceFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.util.Assert;

/**
* XFire Client 工厂??. <p/>
* <p>
* 封装了XFire有点奇???的根据Web服务接口类和Service URL 生成客户端Proxy类的代码. 同时使用泛型,减少了返回时的强制类型转??.
* </p>
*
* @author calvin
*/
@SuppressWarnings("unchecked")
public class XFireClientFactory implements FactoryBean {
private static XFireProxyFactory serviceFactory = new XFireProxyFactory();

private static final Log log = LogFactory.getLog(XFireClientFactory.class);

private String serviceURL;

private String serviceClassName;

private XFireClientFactory() {
}

public Object getObject() throws Exception {
   String url = this.getServiceURL();
   Class sClass = null;
   try {
    sClass = Class.forName(this.getServiceClassName());
   } catch (ClassNotFoundException e) {
    log.error(e.getMessage(), e);
    return null;
   }
   Assert.notNull(url);
   Assert.notNull(sClass);
   Service serviceModel = new ObjectServiceFactory().create(sClass);
   try {
    return serviceFactory.create(serviceModel, url);
   } catch (MalformedURLException e) {
    log.error(e.getMessage(), e);
    return null;
   }
}

public Class getObjectType() {
   Class sClass = null;
   try {
    sClass = Class.forName(this.getServiceClassName());
   } catch (ClassNotFoundException e) {
    log.error(e.getMessage(), e);
    return null;
   }
   return sClass;
}

public boolean isSingleton() {
   return true;
}

/**
* 获得POJO形式的Web服务的客户端Proxy??.
*
* @param serviceURL
*            Web ServiceURL
* @param serviceClass
*            Web Service接口??
* @return 类型为Web Service接口的客户端Proxy??
*/
public static <T> T getClient(String serviceURL, Class<T> serviceClass) {
   Assert.notNull(serviceURL);
   Assert.notNull(serviceClass);
   Service serviceModel = new ObjectServiceFactory().create(serviceClass);
   try {
    return (T) serviceFactory.create(serviceModel, serviceURL);
   } catch (MalformedURLException e) {
    log.error(e.getMessage(), e);
    return null;
   }
}

/**
* 获得JSR181形式定义的Web服务的客户端Proxy??. 注意和普通的生成客户端方法不??,??要多??个implClass参数.
*
* @param serviceURL
*            Web ServiceURL.
* @param serviceClass
*            Web Service接口??.
* @param implClass
*            Web Service街口的服务端实现??.
* @return 类型为Web Service接口的客户端Proxy??.
*/
public static <T> T getJSR181Client(String serviceURL,
    Class<T> serviceClass, Class implClass) {
   Assert.notNull(serviceURL);
   Assert.notNull(serviceClass);
   Service serviceModel = new AnnotationServiceFactory().create(implClass);
   try {
    return (T) serviceFactory.create(serviceModel, serviceURL);
   } catch (MalformedURLException e) {
    log.error(e.getMessage(), e);
    return null;
   }
}

public String getServiceURL() {
   return serviceURL;
}

public void setServiceURL(String serviceURL) {
   this.serviceURL = serviceURL;
}

public String getServiceClassName() {
   return serviceClassName;
}

public void setServiceClassName(String serviceClassName) {
   this.serviceClassName = serviceClassName;
}

}

上面这个客户端调用接口类,其实一般都是不变了。每次新增一个调用就新增一个spring的配置就可以了,还是在
applicationContext-webservice.xml这个配置里面增加如下几行:
<bean name="userManagerXfire"
    class="com.sillycat.plugin.webservice.xfire.XFireClientFactory">
   <property name="serviceURL" value="http://www.sillycat.com/easyLife/services/IUserManager"/>
   <property name="serviceClassName" value="com.sillycat.core.webservice.IUserManager"/>
</bean>

serviceURL是提供webservice的地址
serviceClassName是webservice这个类的接口。呵呵。这种方法调用默认要把接口类打包到调用webservice那边去哈。
接口发布给别人不算泄密吧。





  相关解决方案