Spring GWT Integration using the RequestFactory API
Beginning from GWT 2.4 the integration of the RequestFactory API with Spring services on the backend is easy all you need to do is create a custom ServiceLocator on your server which will be used by GWT to locate properly the called services :
public class SpringServiceLocator implements ServiceLocator {
public Object getInstance(Class<?> clazz) {
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(
RequestFactoryServlet.getThreadLocalServletContext());
return context.getBean(clazz);
}
}
The second step is to declare you RequestFactory servlet on your web.xml like this, (I assume that you have already spring set up) :
<servlet> <servlet-name>requestFactoryServlet</servlet-name> <servlet-class>org.gxpenses.util.SpringRequestServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>requestFactoryServlet</servlet-name> <url-pattern>/gwtRequest</url-pattern> </servlet-mapping>
As usual on the GWT part you have to configure your Proxies (because you use service style backend, you have to use ValueProxy instead of the EntityProxy) and your Requests which are the remote interfaces of your services :
//Note that I inherit from the ValueProxy object
@ProxyFor(Account.class)
public interface AccountProxy extends ValueProxy {
public String getId();
public void setId(String id);
public String getName();
public void setName(String name);
public Double getBalance();
public void setBalance(Double balance);
public String getType();
public void setType(String type);
}
//You have to provide you service Impl class, and the ServiceLocator you created
//Note that Account is automatically to AccountProxy on the client
@Service(value=AccountsServiceImpl.class, locator=SpringServiceLocator.class)
public interface AccountRequest extends RequestContext {
abstract Request<Void> createNewAccount(AccountProxy account);
abstract Request<Void> updateAccountBalance(String accountId, Double transactionAmount, String type);
abstract Request<Double> totalAmountByAccountAndPeriodeAndType(String accountId, Date start, Date end, String type);
}
That’s it for the integration, for more informations on how to use the RequestFactory API see : RequestFactory API

Can you show us the sample code of org.gxpenses.util.SpringRequestServlet ?
dahlong
October 26, 2011 at 7:46 am
Sweet. Thanks!
Yaniv
October 29, 2011 at 5:03 am
Great info. Thanks.
gal bracha
January 22, 2012 at 7:55 am
Thanks for the great post, but can u send or post the org.gxpenses.util.SpringRequestServlet servlet file?
Thanks
Barny
March 6, 2012 at 3:08 pm
Post the source
Alexandre Cassimiro Andreani
June 22, 2012 at 5:03 pm
Hi there, just became aware of your blog through Google, and found that it’s really informative. I am gonna watch out for brussels. I’ll be grateful if you continue this
in future. A lot of people will be benefited from your writing.
Cheers!
helpful hints
August 7, 2012 at 2:37 am
Great, I’m preparing a bunch of articles about GWT / GWTP soon.
All from my accumulated experience and best practices I have learned over time.
imrabti
August 8, 2012 at 2:55 pm
that could be useful to have all the code…
redfox26
August 17, 2012 at 5:50 pm
I was working on an GWT / Spring full example, I didn’t have enought time to finish but there is a partial version I uploaded, It shows clearly how everything is integrated : https://www.dropbox.com/s/1arcq7qea6ym8rd/expenses.zip
imrabti
September 13, 2012 at 10:20 am
Using spring annotation for dao and service don’t seem to work. we need to declare it in the xml file…
Marc Collin
September 13, 2012 at 9:51 am
It does work, I’ll add here a complete example showing how it is integrated.
imrabti
September 13, 2012 at 9:59 am
hi, can you please provide a full example of this?
i’m trying but it doesnt works at all.
xober
March 28, 2013 at 3:35 pm
I have create a Spring Archetype to generate this kind of project, with a complete configuration : https://github.com/ArcBees/ArcBees-tools/tree/master/archetypes
imrabti
April 19, 2013 at 11:08 am