Fancy UI

All About Crazy IT Stuff

Spring GWT Integration using the RequestFactory API

with 13 comments

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

About these ads

Written by imrabti

September 23, 2011 at 8:21 am

Posted in GWT

Tagged with , ,

13 Responses

Subscribe to comments with RSS.

  1. Can you show us the sample code of org.gxpenses.util.SpringRequestServlet ?

    dahlong

    October 26, 2011 at 7:46 am

  2. Sweet. Thanks!

    Yaniv

    October 29, 2011 at 5:03 am

  3. Great info. Thanks.

    gal bracha

    January 22, 2012 at 7:55 am

  4. 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

  5. Post the source

    Alexandre Cassimiro Andreani

    June 22, 2012 at 5:03 pm

  6. 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

  7. that could be useful to have all the code…

    redfox26

    August 17, 2012 at 5:50 pm

  8. 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

  9. 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


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: