Using the GWTController
 

Important Note: This post refers to GWT-SL 0.1.3. The GWTController was removed after that and replaced by the GWTRPCServiceExporter.

 

In the third  part of this series after Using the GWTSpringController and Using the GWTHandler we will have a look at the GWTController which is a slightly more convenient version of the (deprecated) GWTSpringController: a runtime wrapper around a single service bean which is exported to an URL via RPC.

The main difference form the GWTSpringController is that you don't extend any special base class now - the GWTController acts similar to a service exporter and publishes a bean to an URL. Contrary to the GWTHandler, the GWTController does not manage those URLs on its own but relies to a different handler, such as the SimpleURLHandlerMapping which most people use in their Spring application.

The Demo WAR contains a simplistic web application that demonstrates in it's most crude charm how both the GWTHandler and the GWTController are used - no matter which you use, a GWT client does not notice any difference, all it cares about is a URL which talks RPC.

Exporting a bean to RPC is rather easy then:

Define the service interface

Note that service interfaces must extend the RemoteService GWT interface just like you would do with a regular RPC service:

public interface ServiceAdd extends RemoteService{
int add(int a, int b);

}

Implement the service

public class ServiceAddImpl implements ServiceAdd {
    public int add(int a, int b) {
    return new Integer(a + b);
    }
}

Register Spring in your web.xml

 This step you would undertake anyway for every Spring application:

 <servlet>
        <servlet-name>controller</servlet-name>
        <servlet-class>            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
 </servlet>

 <servlet-mapping>
        <servlet-name>controller</servlet-name>
        <url-pattern>/controller/*</url-pattern>
 </servlet-mapping>

 Register your controller

... in the appropriate servletname-servlet.xml

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <map>
            <entry key="/add.rpc" value-ref="ControllerAdd" />

...
        </map>
    </property>
</bean>
   
<bean id="ControllerAdd" class="org.gwtwidgets.server.spring.GWTController">
    <property name="service" ref="ServiceAdd"/>
</bean>


This way one can map multiple beans with the same URL handler to different (sub) URLs. In our example the complete URL to the service would be 

http://localhost:8080/gw-wl-sl/controller/add.rpc


The GWTController has the same interesting properties we've already seen on the GWTHandler:

  • usingInstrumentation which either uses CGLib (false) or Javassist (true)
  • classEnhancer which allows you to set your own enhancer in case you are not happy with either the CGLib or the Javassist implementations
  • service is a new one: it is the bean to which RPC will be delegated
  • unwrappingGWTExceptions which by default (true) checks an exceptions' stack trace for occurrences of a SerializableException and throws this instead of any other surrounding exceptions. This is in general known as Exception translation and preserves here GWT's contract of propagating SerializableException to the client and throwing it in the servlet container.