Archive

Posts Tagged ‘gwt’

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;

	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		applicationContext = WebApplicationContextUtils.
          getRequiredWebApplicationContext(config.getServletContext());
	}

	@Override
	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
					.getClass());

			// invocation
			return RPC.invokeAndEncodeResponse(service, rpcRequest.getMethod(),
					rpcRequest.getParameters());
		} 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 »:

	<servlet>

		<servlet-name>GWT-RPC</servlet-name>

		<servlet-class>com.elosystemes.gwt.GWTRPCServlet</servlet-class>

	</servlet>

	<servlet-mapping>

		<servlet-name>GWT-RPC</servlet-name>

		<url-pattern>*.rpc</url-pattern>

	</servlet-mapping>

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 :

@RemoteServiceRelativePath("remoteService.rpc")
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 :

  @Service("remoteService")
  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…

Publicités
Catégories :Uncategorized Étiquettes : , , ,