Archive for septembre 2009

How to expose Spring beans as GWT rpc services

25 septembre 2009 Laisser un commentaire

1) create this servlet :

public class GWTRPCServlet extends RemoteServiceServlet {

	private static final long serialVersionUID = 8940035753925233840L;

	private ApplicationContext applicationContext;

	public void init(ServletConfig config) throws ServletException {
		applicationContext = WebApplicationContextUtils.

	public String processCall(String payload) throws SerializationException {

		try {
			// find service using URI

			String serviceName = getThreadLocalRequest().getRequestURI()
					.replaceAll(".*/([^/]+)\\.rpc$", "$1");
			Object service = applicationContext.getBean(serviceName);

			// decode request
			RPCRequest rpcRequest = RPC.decodeRequest(payload, service

			// invocation
			return RPC.invokeAndEncodeResponse(service, rpcRequest.getMethod(),
		} catch (Exception e) {
			return RPC.encodeResponseForFailure(null, e);

2) declare this servlet in your web.xml to catch all the requests that end with « .rpc »:









3) when you declare a service client in GWT, use the spring bean name + « .rpc » as the remote path of the service, just as above :

public interface HelloworldService extends RemoteService {
     public void hello(String name);

4) on server side, just declare the implementing bean as usual in your application context :

  public class HelloworldServiceImpl implements HelloService {
      public void hello(String name) {
           System.out.prinlnt("Hello, "+name);

This is the simpliest way i’ve found for interfacing Spring and the GWT RPC layer…

Catégories :Uncategorized Étiquettes : , , ,