我们的项目用到了xmlrpc,不过还是用的2.x版本的。由于xmlrpc3.x地推出。提供了NULL,Serializable等的支持,将原来的Hashtable改成了Map,Vector改成了List。都是不错的进步。所以我们决定从xmlrpc2.x升级到xmlrpc3.x.
在spring里面有几个ServiceExporter,org.springframework.remoting.rmi.RmiServiceExporter、org.springframework.remoting.caucho.HessianServiceExporter、org.springframework.remoting.caucho.BurlapServiceExporter。不过没有xmlrpc的serviceExporter,原来我们是自己封装的XmlRpcServer,用servlet提供服务。(eg:http://localhost:8080/community/service/xmlrpc)没有和spring集成虽然用了spring。
考虑到spring的便利以及配置的同意我决定将xmlrpcService放入spring中。xmlrpc3.x和xmlrpc2.x的代码基本上没有一样的。改了很多东西。除了类型变化之外,还添加了对异常的支持。详细信息请参照xmlrpc3.x源代码。
XmlRpcServiceExporter.java
import javax.servlet.ServletException;
/**
* @author <a href="mailto:rory.cn@gmail.com">somebody</a>
* @since 2006-9-27 03:59:22 pm
* @version $Id XmlRpcServiceExporter.java$
*/
public class XmlRpcServiceExporter extends RemoteExporter implements
Controller, InitializingBean {
private XmlRpcServletServer server;
public String serviceName;
public Resource configFile;
public Boolean enabledForExtensions;
public void setEnabledForExtensions(Boolean enabledForExtensions) {
this .enabledForExtensions = enabledForExtensions;
}
public void setConfigFile(Resource configFile) {
this .configFile = configFile;
}
public String getServiceName() {
return serviceName;
}
public void setServiceName(String serviceName) {
this .serviceName = serviceName;
}
public XmlRpcServletServer getXmlRpcServletServer() {
return server;
}
/* (non-Javadoc)
* @see org.springframework.web.servlet.mvc.Controller#handleRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
if ( ! WebContentGenerator.METHOD_POST.equals(request.getMethod())) {
throw new ServletException( " XmlRpcServiceExporter only supports POST requests " );
}
server.execute(request, response);
return null ;
}
/* (non-Javadoc)
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
*/
public void afterPropertiesSet() throws Exception {
server = new XmlRpcServletServer();
server.setHandlerMapping(newXmlRpcHandlerMapping());
if (enabledForExtensions != null ) {
((XmlRpcServerConfigImpl) server.getConfig()).setEnabledForExtensions(enabledForExtensions.booleanValue());
}
}
/** Creates a new handler mapping. The default implementation loads
* a property file from the resource
*
*/
protected XmlRpcHandlerMapping newXmlRpcHandlerMapping() throws XmlRpcException {
SpringHandlerMapping mapping = new SpringHandlerMapping(getServiceInterface());
mapping.addHandler(getServiceName(), getServiceInterface());
mapping.setTagetObject(getProxyForService());
return mapping;
}
}
spring配置文件
















< property name ="mappings" >
< props >
< prop key ="/account" > rpcAccountService </ prop >
</ props >
</ property >
</ bean >
< param-name > contextConfigLocation </ param-name >
< param-value >
classpath:spring/global.xml
</ param-value >
</ context-param >
< listener >
< listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class >
</ listener >
< servlet >
< servlet-name > service </ servlet-name >
< servlet-class > org.springframework.web.servlet.DispatcherServlet </ servlet-class >
</ servlet >
< servlet-mapping >
< servlet-name > service </ servlet-name >
< url-pattern > /service/xmlrpc3/* </ url-pattern >
</ servlet-mapping >
希望对大家有用,这里提供project下载。包含一个client程序。com.jdkcn.xmlrpc.Client


署名,非商业用途,保持一致. somebody(莫多)
