Platinum Partner

Spring REST Services with GWT

For my own interest I started exploring Spring REST Services with GWT. It took some time to figure it out and then I came accross RestyGWT. With the help of RestyGWT, I managed to integrate GWT with Spring REST Services. My idea was to keep the GWT way of creating the service and serviceAsync and yet make a REST call. Below are the steps which will help achieve above.

My preffered development environment is Eclipse, so as a prerequisite you must have Eclipse with Maven support installed.

Lets Begin,

  1. Create a Maven project, goto File-->New-->Other. In the wizard type "Maven". Select Maven Project and click on Next.
  2. In the "Select project name and location" page of the wizard, make sure that "Create a simple project (skip archetype selection)" option is checked, hit "Next" to continue with default values.
  3. In the "Enter group id for the artifact" page of the wizard, enter values for group id and artifactid. Select the packaging as "war", hit "Finish" to exit the wizard and to create your project.
Modify the POM file to add required dependencies as below
<project xmlns="" xmlns:xsi=""

"Update Project Configuration" by Right clicking on your project-->Maven.

The below steps assumes that your are aware of the GWT project structure. Create your GWT module. This can be done by installing GWT plugin for Eclipse. Once the GWT module is ready update your <Module>.gwt.xml with the below given content.

<?xml version="1.0" encoding="UTF-8"?>
<module rename-to='restgwt'>
<!-- Inherit the core Web Toolkit stuff. -->
<inherits name='' />
<inherits name='org.fusesource.restygwt.RestyGWT' />
<inherits name='' />

<!-- Specify the app entry point class. -->
<entry-point class='com.sagar.restgwt.client.RestGWT' />

<!-- Specify the paths for translatable code -->
<source path='client' />
<source path='shared' />

In the client package create the service to make REST Call. With this approach we dont have to create the ServiceAsync interface. We will be creating our service interface by extending the "RestService", provided by RestyGWT

Code Snippet: 

InfoService.Java [ A service interface to make REST call. ]

public interface InfoService extends RestService {
public static class Util {
private static InfoService instance;
public static InfoService getService() {
if (instance == null) {
instance = GWT.create(InfoService.class);
Resource resource = new Resource(GWT.getModuleBaseURL() + "service");
        ((RestServiceProxy) instance).setResource(resource);

return instance;

public void getInfo(MethodCallback<OrderConfirmation> callback);
} [ Model which will be returned as a response. ]

public class OrderConfirmation {
    public String message;
    public Long ready_time;

     * Example of how to create an instance of a JsonEncoderDecoder for a data
     * transfer object.
    public interface OrderConfirmationJED extends JsonEncoderDecoder<OrderConfirmation> {

    public String toString() {
        if (GWT.isClient()) {
            OrderConfirmationJED jed = GWT.create(OrderConfirmationJED.class);
            return jed.encode(this).toString();

        return super.toString();
} [ GWT module entrypoint to see things running. ]

public class RestGWT implements EntryPoint {
public void onModuleLoad() {
Button button = new Button("Click Me");
button.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
InfoService.Util.getService().getInfo(new MethodCallback<OrderConfirmation>() {
public void onSuccess(Method method, OrderConfirmation response) {
RootPanel.get().add(new Label(response.toString()));

public void onFailure(Method method, Throwable exception) {


Create the Spring managed controller as below, this should be presnt in the "server" package as per GWT project structure. Also you can notice that we don't have to implement our service interface. [ Spring managed controller. ]

public class RestGWTController {
@RequestMapping(value = "/loadInfo", method = RequestMethod.GET, headers = "Accept=application/json")
public @ResponseBody OrderConfirmation handleRequest(HttpServletRequest request,
  HttpServletResponse response) throws ServletException, IOException {
GreetingServiceEndpoint endpoint = greetingService.getGreetingServiceEndpointPort();

OrderConfirmation confirmation = new OrderConfirmation();
confirmation.message = endpoint.sayHello();
        confirmation.ready_time = System.currentTimeMillis() + 1000 * 60 * 30;

return confirmation;

Settings required for Spring


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi=""
xmlns="" xmlns:web=""

<!-- Name the application -->
<display-name>Rest GWT</display-name>
<description>This is web-project for RestGWT</description>

<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>


<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns=""

<!-- registers all of Spring's standard post-processors for annotation-based configuration -->
<context:annotation-config />


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
xmlns:p="" xmlns:xsi=""

<!-- Scans the classpath of this application for @Components to deploy as 
beans -->
<context:component-scan base-package="com.sagar.restgwt.server" />

<!-- Configures the @Controller programming model -->
<mvc:annotation-driven />

<property name="messageConverters">
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />

Now all configuration is completed.

To build the application right click on your project → Run As → Maven Install. This will create the war file in your project's target/restgwt folder. To test this approach we are going to deploy our web application to an Apache Tomcat 7 server.

To launch the application point your browser to the following address



Enjoy Coding


{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ }}
{{ parent.authors[0].realName ||}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks