我们的项目用到了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
package
com.jdkcn.xmlrpc;
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配置文件
<bean id="accountService" class="com.jdkcn.service.impl.AccountServiceImpl">
</bean>
<bean name="rpcAccountService" class="com.jdkcn.xmlrpc.XmlRpcServiceExporter">
<property name="service">
<ref bean="accountService"/>
</property>
<property name="serviceName">
<value>jdkcn.accountService</value>
</property>
<property name="enabledForExtensions">
<value>true</value>
</property>
<property name="serviceInterface">
<value>com.jdkcn.service.AccountService</value>
</property>
</bean> 然后映射一个地址就可以通过xmlrpc访问服务了
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/account">rpcAccountService</prop>
</props>
</property>
</bean>
web.xml
<context-param>
<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>
然后我们的service地址就是这样的http://localhost:8080/service/xmlrpc3/account
希望对大家有用,这里提供project下载。包含一个client程序。com.jdkcn.xmlrpc.Client
点击下载完整代码 除经特别注明外,本文章版权归
莫多泡泡所有.
署名,非商业用途,保持一致. somebody(莫多)
posted on 2006-10-22 16:12
莫多 阅读(2168)
评论(0) 编辑 收藏 所属分类:
Spring